【问题标题】:How to Run or test my Lambda function through cloudformation如何通过 cloudformation 运行或测试我的 Lambda 函数
【发布时间】:2019-11-02 05:47:46
【问题描述】:

我创建了一个 lambda 函数并通过 cloudformation 进行部署。 我希望在创建 cloudformation 堆栈后自动执行 lambda 函数。

【问题讨论】:

  • 抱歉,您需要先向我们展示您尝试了什么以及您是如何进行设置的。如果我们看不到您到目前为止所做的工作,我们将无法帮助您。
  • 当您说“在创建 CloudFormation 堆栈之后”时,是否可以让它在堆栈创建“期间”执行?这种方法的好处是,如果 Lambda 函数返回错误,CloudFormation 堆栈可以发出失败信号并触发自动回滚。此外,当堆栈被删除时,可以再次调用 Lambda 函数进行清理处理。见:AWS Lambda-backed Custom Resources - AWS CloudFormation

标签: amazon-cloudformation


【解决方案1】:

我希望触发这个 lambda 并获取 instane-id 的值作为输出

【讨论】:

    【解决方案2】:
    
    AWSTemplateFormatVersion: "2010-09-09"
    Resources:
      Roleforlambda:
        Type: "AWS::IAM::Role"
        Properties: 
          AssumeRolePolicyDocument: 
            Version: "2012-10-17"
            Statement: 
              - 
                Effect: "Allow"
                Principal: 
                  Service: 
                    - "lambda.amazonaws.com"
                Action: 
                  - "sts:AssumeRole"
      RolePolicies: 
        Type: "AWS::IAM::Policy"
        Properties: 
          PolicyName: "root"
          PolicyDocument: 
            Version: "2012-10-17"
            Statement: 
              - 
                Effect: "Allow"
                Action: "*"
                Resource: "*"
          Roles: 
            - 
              Ref: "Roleforlambda"
      lambdaFunction1:   
        Type: AWS::Lambda::Function
        Description: For getting Instance ID
        Properties:
          Handler: index.lambda_handler
          Role: !GetAtt Roleforlambda.Arn
          Code: 
            ZipFile: !Sub |
              import json
              import boto3  
              import cfnresponse
    
              def create_key_pair(instanceid):
                ec2 = boto3.client('ec2', 'us-east-1')
                response = ec2.describe_instances()
                for reservation_data in response['Reservations']:
                  for instance_data in reservation_data['Instances']:
                      for tags_data in instance_data['Tags']:
                         print(instance_data['InstanceId'])
    
              def lambda_handler(event, context):
    
                try:
                  if event['RequestType'] == 'Delete':
                    print("delete called")
                    response = 'SUCCESS'
                else:
                  print("create called")
                  create_key_pair(instanceid)
                  response = 'SUCCESS'
                cfnresponse.send(event, context, cfnresponse.SUCCESS)    
                return response
          Runtime: python3.7
          Timeout: 200
    
      MyFrontEndTest: 
        Type: "Custom::Lambdatrigger"
        Properties: 
          ServiceToken: !GetAtt lambdaFunction1.Arn
          key: return x
    Outputs: 
      CustomResourceAttribute1: 
        Value: !Ref MyFrontEndTest
    

    【讨论】:

      猜你喜欢
      • 2019-07-28
      • 1970-01-01
      • 2021-05-26
      • 1970-01-01
      • 1970-01-01
      • 2018-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多