通过云形成创建并执行aws lambda函数

时间:2017-03-28 18:02:45

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

我正在通过云形成模板创建我的lambda函数。 我的问题是,在创建这个lambda资源之后,我想将几​​个变量作为输入传递并立即执行。有没有办法通过云形成模板来实现?

AWSTemplateFormatVersion: '2010-09-09'
Description: Create a lambda function for chef rds read replica
Parameters:
  Environment:
    Description: Environment that will be built
    Type: String
    Default: q1
    AllowedValues:
    - q1
Mappings:
  ChefEnvironmentMap:
    q1:
      IAMRole: CHEFAWS-RDS
      Subnets:
      - subnet-***
      SecurityGroups:
      - sg-***
      NetCoreEnvironment: qa
Resources:
  ChefRDSReadOnlyReplica:
    Type: AWS::Lambda::Function
    Properties:
      Code:
        S3Bucket: cheftestbucket
        S3Key: lambda/cheflambda.zip
      Description: "Chef rds"
      Environment:
        Variables:
          http_proxy: 'http://**'
          https_proxy: 'http://**'
          no_proxy: '169.254.169.254,127.0.0.1,localaddress,.localdomain.com'
      Handler: createreadreplica.lambda_handler
      MemorySize: 128
      Role: arn:aws:iam::*****:role/CHEFAWS-RDS
      Runtime: python2.7
      Timeout: 60
      VpcConfig:
        SecurityGroupIds: !FindInMap [ChefEnvironmentMap, !Ref Environment, SecurityGroups]
        SubnetIds: !FindInMap [ChefEnvironmentMap, !Ref Environment, Subnets]

1 个答案:

答案 0 :(得分:1)

我按照方法创建了一个自定义资源来执行lambda功能。我也通过cft启动了自定义资源,就像这样。

  Lambdaresource:
    DependsOn: ChefRDSReadOnlyReplica
    Properties:
      sourceregion: "us-east-1"
      target_region: "us-west-2"
      db_instance_id: "chef-tod-pg-rds"
      dbsubnet_groupname: "******"
      kms_keyid: "******"
      ServiceToken: !GetAtt ChefRDSReadOnlyReplica.Arn
    Type: AWS::CloudFormation::CustomResource

然而,这导致更复杂。由于自定义资源的云形成在触发lambda函数后不会得到通知。所以它仍然处于“创造资源”状态。为了解决这个问题,我必须在我的lambda Python代码中创建一个返回成功/失败的新函数。如上所述:http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-lambda-function-code.html