如何使用 golang 为 lambda 函数提供配置值
How to provide config values for lambda functions using golang
如何提供配置文件以及用 golang 编写的 lambda 函数。我在项目根目录中创建了一个名为 config 的文件夹,并提供了 config.But 不幸的是,在项目获得 build.And 后它不起作用我知道这是因为我们将其构建为二进制文件 file.So],为 golang 提供配置文件和 lambda 函数的最佳方法是什么?
var config map[string]string
var filename string
//Checks the stage and loads the Configuration file
if len(event.Stage) > 0 {
filename = "configs/config.json"
}
configFile, err := os.Open(filename)
defer configFile.Close()
if err != nil {
log.Printf(err.Error())
return config, err
}
jsonParser := json.NewDecoder(configFile)
err = jsonParser.Decode(&config)
return config, err
将配置文件添加到包含 Go 二进制文件的 zip 文件中
例如,假设您有一个 Handler.go,其配置目录包含 configs.json 然后
go build -o Handler Handler.go && chmod +x Handler && zip -r handler.zip Handler configs
应该制作一个包含 configs 目录和可以上传到 Lambda 的处理程序二进制文件的 zip
您可能希望考虑使用 AWS Parameter Store 等文件的替代方法
如何提供配置文件以及用 golang 编写的 lambda 函数。我在项目根目录中创建了一个名为 config 的文件夹,并提供了 config.But 不幸的是,在项目获得 build.And 后它不起作用我知道这是因为我们将其构建为二进制文件 file.So],为 golang 提供配置文件和 lambda 函数的最佳方法是什么?
var config map[string]string
var filename string
//Checks the stage and loads the Configuration file
if len(event.Stage) > 0 {
filename = "configs/config.json"
}
configFile, err := os.Open(filename)
defer configFile.Close()
if err != nil {
log.Printf(err.Error())
return config, err
}
jsonParser := json.NewDecoder(configFile)
err = jsonParser.Decode(&config)
return config, err
将配置文件添加到包含 Go 二进制文件的 zip 文件中
例如,假设您有一个 Handler.go,其配置目录包含 configs.json 然后
go build -o Handler Handler.go && chmod +x Handler && zip -r handler.zip Handler configs
应该制作一个包含 configs 目录和可以上传到 Lambda 的处理程序二进制文件的 zip
您可能希望考虑使用 AWS Parameter Store 等文件的替代方法