Ishu Gupta
Ishu Gupta

Reputation: 1091

Create and Execute an aws lambda function through cloud formation

I am creating my lambda function like following through cloud formation template. My question is , after creating this lambda resource , I want to pass it few variables as an input and execute it immediately. Is there a way I can do it through cloud formation template?


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]

Upvotes: 1

Views: 2394

Answers (1)

Ishu Gupta
Ishu Gupta

Reputation: 1091

I followed the approach to create a custom resource to execute the lambda fuction. I launched the custom resource through the cft as well, like this.

  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

However, this lead to more complexity . As cloud formation of custom resource doesnt get notified after it triggers the lambda function. So it remains in “Creating Resource” State. To handle this , I had to create a new function in my lambda Python code that returned the success/failure. As mentioned here: http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-lambda-function-code.html

Upvotes: 1

Related Questions