gpt4 book ai didi

aws-cloudformation - 如何通过 cloudformation 运行或测试我的 Lambda 函数

转载 作者:行者123 更新时间:2023-12-03 07:48:19 27 4
gpt4 key购买 nike

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

最佳答案


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

关于aws-cloudformation - 如何通过 cloudformation 运行或测试我的 Lambda 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56668482/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com