【问题标题】:How to show lambda function return values in Outputs of Cloudformation如何在 Cloudformation 的输出中显示 lambda 函数返回值
【发布时间】:2018-11-04 21:30:13
【问题描述】:

我正在寻找在 Cloudformation 的输出中列出 AWS::Lambda::Function 的结果的选项。

以下是 AWS:Lambda::Function 的 cloudformation 模板的 sn-p

Resources:
 AthenaLambdaFunction:
    Type: 'AWS::Lambda::Function'
    DeletionPolicy: Delete
    DependsOn:
      - IamRoleLambdaForAthena
    Properties:
      Code:
        ZipFile: |
          import boto3
          import botocore
          import os 
          ath = boto3.client('athena')
          def handler(event, context):
              outputBucket = os.environ.get("outputBucket")
              QSTRING = 'select * from tableName limit 10'
              response = ath.start_query_execution(QueryString=str(QSTRING), ResultConfiguration={'OutputLocation': outputBucket})
              s3BucketOut = output_bucket + response['ResponseMetadata']['RequestId']
              return s3BucketOut
      Handler: index.handler
      Runtime: python3.6
      MemorySize: 128
      Role: !GetAtt IamRoleLambdaForAthena.Arn
      Timeout: 30
      Environment:
        Variables:
          outputBucket: !Ref OutputS3Bucket

我想在 Cloudformation 的输出中显示由 lambda 函数 s3BucketOut 重新调整的值。如下所示(当然,下面的代码不起作用)。

Outputs:
  LambdaFunctionOutput:
    Value: !Ref AthenaLambdaFunction.s3BucketOut
    Description: Return Value of Lambda Function

请有任何建议。 TIA

【问题讨论】:

    标签: aws-lambda amazon-cloudformation


    【解决方案1】:

    你已经完成了一半。使用您的代码,您创建了要运行的 AWS Lambda 函数。现在您需要让这个函数在 CloudFormation 上运行并捕获它的值。请注意,您需要对代码进行少量更改,以允许 CloudFormation 捕获该值。

    完整的代码将与此类似:

    Resources:
      AthenaLambdaFunction:
        Type: 'AWS::Lambda::Function'
        DeletionPolicy: Delete
        DependsOn:
          - IamRoleLambdaForAthena
        Properties:
          Code:
            ZipFile: |
              import boto3
              import botocore
              import os
              import cfnresponse # this needs to be imported for replying to CloudFormation
              ath = boto3.client('athena')
              def handler(event, context):
                  outputBucket = os.environ.get("outputBucket")
                  QSTRING = 'select * from tableName limit 10'
                  response = ath.start_query_execution(QueryString=str(QSTRING), ResultConfiguration={'OutputLocation': outputBucket})
                  s3BucketOut = output_bucket + response['ResponseMetadata']['RequestId']
                  responseData = {} # added
                  responseData['S3BucketOut'] = s3BucketOut # added
                  cfnresponse.send(event, context, cfnresponse.SUCCESS, responseData) # return modified
          Handler: index.handler
          Runtime: python3.6
          MemorySize: 128
          Role: !GetAtt IamRoleLambdaForAthena.Arn
          Timeout: 30
          Environment:
            Variables:
              outputBucket: !Ref OutputS3Bucket
    
      S3BucketOutInvocation:
        Type: Custom::S3BucketOut
        Properties:
          ServiceToken: !GetAtt AthenaLambdaFunction.Arn
          Region: !Ref "AWS::Region"
    
    Outputs:
      LambdaFunctionOutput: 
        Value: !GetAtt S3BucketOutInvocation.S3BucketOut
        Description: Return Value of Lambda Function
    

    参考资料:

    https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/template-custom-resources.html https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-lambda-function-code.html

    【讨论】:

    • 这不起作用,因为这个错误Template format error: Unresolved resource dependencies [S3BucketOut] in the Resources block of the template
    【解决方案2】:

    您可以做的是创建所谓的“Lambda 支持的自定义资源”,您可以在 Stack 创建期间使用它来获取创建类型的信息。

    更多信息可以在这里找到

    AWS Lambda-backed Custom Resources

    【讨论】:

      猜你喜欢
      • 2022-12-31
      • 2015-06-16
      • 1970-01-01
      • 2020-11-05
      • 2019-04-03
      • 2018-10-31
      • 2019-01-20
      • 1970-01-01
      • 2019-10-27
      相关资源
      最近更新 更多