【问题标题】:Unable to set environment variable for aws lambda function executed as AWS::CloudFormation::CustomResource无法为作为 AWS::CloudFormation::CustomResource 执行的 aws lambda 函数设置环境变量
【发布时间】:2018-08-30 15:11:15
【问题描述】:

我正在尝试通过我的 CloudFormation 模板 json 为 CustomResource 设置在运行时取值的环境变量。这样稍后它会执行一个 python lambda,我可以读取 lambda 中的环境变量并处理一些数据。

我希望我的 python lambda 能够在 os.environ 中读取这个变量

以下是我的 Cloudformation for CustomResource

"TriggerRedshiftSetupLambda": {
      "Type": "AWS::CloudFormation::CustomResource",
      "Version": 1.0,
      "Properties": {
       "Environment": {
          "Variables": {
            "AHost": {
                "Fn::GetAtt" : [ "A", "Endpoint.Address" ]
            },
            "APort": {
                "Fn::GetAtt" : [ "A", "Endpoint.Port" ]
            }
         }
        },
        "ServiceToken": {
          "Fn::GetAtt" : [ "ASetupLambda", "Arn" ]
        }
      }
    }

这是我使用变量的 lambda 代码

def lambda_handler(event, context):
    print(os.environ)
    print(os.environ['AHost'])

第一个打印语句打印整个环境变量列表,但没有任何“AHost”的键/值对

我做错了吗?如何通过customresource for lambda正确初始化环境变量?

【问题讨论】:

  • 您是否有理由必须使用自定义资源而不是 AWS::Lambda::Function
  • 是的,因为必须触发 lambda 作为 cloudformation 部署的依赖进程才能设置 Redshift 表和架构

标签: amazon-web-services environment-variables aws-lambda amazon-cloudformation


【解决方案1】:

似乎不支持通过自定义资源定义设置环境变量。您设置的是实际调用的属性部分(即事件数据)。

因此,使用您的模板,您的配置应该可以通过以下路径访问。

event['ResourceProperties']['Environment']['Variables']['AHost']

【讨论】:

  • 我会检查一下,但是我希望它可以在 os.environ['AHost'] 下按照标准使用
【解决方案2】:

正如上面@jens 所述,无法使用 CustomResource CloudFormation 在 os.environ 下设置环境变量。

相反,Lambda CloudFormation 需要定义这些值 -

"RedshiftSetupLambda": {
  "Type": "AWS::Lambda::Function",
  "Properties": {
    "Code": {
      "S3Bucket": { "Fn::Sub": "XYZ-${Branch}" },
      "S3Key": { "Fn::Sub": "XYZ-${Commit}.zip" }
    },
    "Description": "Setup Lambda",
    "FunctionName": { "Fn::Sub": "${BlockId}-setup-${Branch}" },
    "Handler": "setup.lambda_handler",
    "KmsKeyArn": {
      "Fn::ImportValue": {
        "Fn::Sub": "${BlockId}-Common-RolesKeys-${Branch}-KMSKeyArn"
      }
    },
    "Role": {
      "Fn::ImportValue": {
        "Fn::Sub": "${BlockId}-Common-RolesKeys-${Branch}-LambdaIAMRoleArn"
      }
    },
    "Runtime": "python2.7",
    "Timeout": 30,
    "VpcConfig": {
      "SecurityGroupIds": [ {"Ref": "SecurityGroup"} ],
      "SubnetIds": [
        { "Fn::ImportValue": "VPCCreate-PrivateSubnet1Id" },
        { "Fn::ImportValue": "VPCCreate-PrivateSubnet2Id" },
        { "Fn::ImportValue": "VPCCreate-PrivateSubnet3Id" }
      ]
    },
    "Environment": {
      "Variables": {
        "DB_USERNAME": {
          "Ref": "MasterUsername"
        },
        "AHOST": {
          "Fn::GetAtt": ["RedshiftCluster", "Endpoint.Address"]
        },
        "APORT": {
          "Fn::GetAtt": ["RedshiftCluster", "Endpoint.Port"]
        },
        "CLUSTER_IDENTIFIER": {
          "Ref": "RedshiftCluster"
        }
      }
    }
  }
}

可以通过以下方式访问它们:

print(os.environ['AHOST'])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-17
    • 1970-01-01
    • 2020-02-04
    • 1970-01-01
    • 2018-10-29
    • 2018-04-30
    • 2017-10-31
    • 2016-01-26
    相关资源
    最近更新 更多