Cloudformation Lambda函数,环境变量不可用,输入不可用

时间:2018-12-04 23:48:43

标签: amazon-web-services amazon-ec2 aws-lambda amazon-cloudformation

不确定我在哪里做错了。以下是我的Lambda代码和cloudformation模板。

下面是我的lambda函数:

import os
import boto3


def quickstart_lambda_handler(event, context):
    print("event={}".format(event)) # nothing shows as k1:v1
    print(os.environ) # nothing show in output as var1
    print("environment variable var1: " + os.environ['var1'])

    json_str = "{\"a1\":\"b1\"}"
    bucket_name = "my-s3-bucket"
    file_name = "qs.json"
    s3_path = "cf-quickstart/" + file_name
    s3 = boto3.resource("s3")
    s3.Bucket(bucket_name).put_object(Key=s3_path, Body=json_str)

    print("object stored in s3")
    return s3_path

我的云形成模板如下所示,用于lambda函数和相关规则。

"QSLERole": {
  "Description" : "Quick Start Lambda Execution Role",
  "Type": "AWS::IAM::Role",
  "Properties": {
    "RoleName" : "QSLERole",
    "AssumeRolePolicyDocument": {
      "Version" : "2012-10-17",
      "Statement": [
        {
          "Effect": "Allow",
          "Principal": {
            "Service": [ "lambda.amazonaws.com" ]
          },
          "Action": [ "sts:AssumeRole" ]
        }]
    },
    "Path": "/",
    "Policies": [
      {
      "PolicyName": "root",
      "PolicyDocument": {
        "Version": "2012-10-17",
        "Statement": [{
          "Effect": "Allow",
          "Action": ["logs:*"],
          "Resource": "arn:aws:logs:*:*:*"
        },
        {
          "Effect": "Allow",
          "Action": "s3:PutObject",
          "Resource": "*"
        }
        ]
      }
    }]
  }
},
"QSLambdaF": {
  "Description" : "Quick Start Lambda Function",
  "Type": "AWS::Lambda::Function",
  "Properties": {
    "Code": {
      "S3Bucket": "my-s3-bucket",
      "S3Key": "generate-quickstartjson.zip"
    },
    "Handler": "generate-quickstartjson.quickstart_lambda_handler",
    "Runtime": "python2.7",
    "Timeout": "30",
    "Environment" : {
      "Variables" : {
        "var1" : "var1 value"
      }
    },
    "Role": {
      "Fn::GetAtt": [
        "QSLERole",
        "Arn"
      ]
    }
  }
},
"ScheduledRule": {
  "Description" :"Cloud Watch Quick Start Event Rule",
  "DependsOn" : [
    "QSLambdaF","EC2Instance"],
  "Type": "AWS::Events::Rule",
  "Properties" : {
    "Description" : "CloudWatch Event for generating output",
    "ScheduleExpression" : "rate(1 minute)",
    "State": "ENABLED",
    "Targets" : [
      {
        "Arn": { "Fn::GetAtt": [
          "QSLambdaF", "Arn"] },
        "Detail" : {
          "test" : "value"
        },
        "Input": "{\"k1\":\"v1\"}",
        "Id" : "QSScheduledRuleV1"
      }
    ]
  }
},
"PEILambda": {
  "Description" : "Permission For Events To Invoke Lambda Function",
  "Type": "AWS::Lambda::Permission",
  "Properties" : {
    "FunctionName": { "Ref": "QSLambdaF"},
    "Action": "lambda:InvokeFunction",
    "Principal": "events.amazonaws.com",
    "SourceArn": { "Fn::GetAtt": [
      "ScheduledRule", "Arn"] }
  }
},

---下面使用安全组创建ec2实例

在lambda lambda中,没有醚类环境变量也没有输入字段。

"Input": "{\"k1\":\"v1\"}",

请让我知道我错了。感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

按原样工作。我使用了错误的模板文件:))