gpt4 book ai didi

python - 需要将 lambda 函数(python)加入 YAML 中的 CloudFormation 模板,我怎样才能实现这一点?

转载 作者:行者123 更新时间:2023-12-03 07:41:21 25 4
gpt4 key购买 nike

我一直在尝试将 lambda 嵌入到这个 CF 模板中。 Lambda 在 YAML 中的 Python 和 CF 上进行编码,我很难将 lambda 转换为 Yaml。

如果您能提供一点帮助,我们将不胜感激。我需要将 lambda 添加到 CF 模板中。任何解释也会有用。

AWSTemplateFormatVersion: 2010-09-09
Description: CH CloudFormation template for Deploy and Init of DynamoDB Table
Parameters:
dbTableName:
Type: String
Default: ClientOrders
Description: Enter the name used for DynamoDB Table
phoneNumber:
Type: String
Default: 1115551111
Description: Enter your phone number

Resources:
OrdersTable:
Type: 'AWS::DynamoDB::Table'
Properties:
TableName: !Ref dbTableName # !Join [ "-", [ !Ref "AWS::StackName", !Ref dbTableName ] ]
AttributeDefinitions:
- AttributeName: PhoneNumber
AttributeType: S
- AttributeName: OrderNumber
AttributeType: S
KeySchema:
- AttributeName: PhoneNumber
KeyType: HASH
- AttributeName: OrderNumber
KeyType: RANGE
TimeToLiveSpecification:
AttributeName: ExpirationTime
Enabled: true
ProvisionedThroughput:
ReadCapacityUnits: '10'
WriteCapacityUnits: '5'
DependsOn:
- DynamoDBQueryPolicy
DynamoDBQueryPolicy:
Type: 'AWS::IAM::Policy'
Properties:
PolicyName: DynamoDBQueryPolicy
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action: 'dynamodb:*'
Resource: '*'
Roles:
- !Ref OrdersTableQueryRole
OrdersTableQueryRole:
Type: 'AWS::IAM::Role'
Properties:
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service:
- dynamodb.amazonaws.com
- lambda.amazonaws.com
Action:
- 'sts:AssumeRole'
Path: /

phoneRecord:
DependsOn:
- OrdersTable
Type: 'Custom::CrtUpdDltDynamodbDocumentLambda'
Properties:
ServiceToken: !GetAtt dynamoSetupLambda.Arn
DynamoTableName: !Ref dbTableName # !Join [ "-", [ !Ref "AWS::StackName", !Ref dbTableName ] ]
# DynamoKeyProperty: PhoneNumber
DynamoItem: !Sub |
[{
"PhoneNumber": "784578745",
"OrderNumber": "11547854",
"someKey": "someValue"
},
{
"PhoneNumber": "${phoneNumber}",
"OrderNumber": "11545154",
"someKey": "someValue"
}]
dynamoSetupLambda:
Type: AWS::Lambda::Function
Properties:
Description: custom resource function invoked for intitial configuration of dynamodb
Handler: index.lambda_handler
Timeout: 300
MemorySize: 128
Role: !GetAtt 'OrdersTableQueryRole.Arn'
Runtime: python3.8
Code:
ZipFile: |
import json, boto3, os, threading, logging
import cfnresponse
def setup_stack(tableName, dynamoItems):
dynamodb_client = boto3.resource("dynamodb", region_name=os.environ['AWS_REGION'])
try:
dynamodb_client_table = dynamodb_client.Table(tableName)
with dynamodb_client_table.batch_writer() as batch:
for message in dynamoItems:
batch.put_item(
Item=message
)
except Exception as exc:
print(exc)
# def remove_stack(tableName): # will need to clear maybe?
# dynamodb_client = boto3.resource(tableName, region_name=os.environ['AWS_REGION'])
# dynamodb_client_table = dynamodb_client.Table(table_name)
# continue
def timeout(event, context):
logging.error('Execution is about to time out, sending failure response to CloudFormation')
cfnresponse.send(event, context, cfnresponse.FAILED, {}, None)
def lambda_handler(event, context):
print('Received event: %s' % json.dumps(event))
# make sure we send a failure to CloudFormation if the function is going to timeout
timer = threading.Timer((context.get_remaining_time_in_millis()
/ 1000.00) - 0.5, timeout, args=[event, context])
timer.start()

status = cfnresponse.SUCCESS
try:
dynamoItems = event['ResourceProperties']['DynamoItem']
dynamoTable = event['ResourceProperties']['DynamoTableName']
if event['RequestType'] == 'Delete':
print("Not going to remvove yet")
# remove_stack(dynamoTable)
else:
setup_stack(dynamoTable, json.loads(dynamoItems))
except Exception as e:
logging.error('Exception: %s' % e, exc_info=True)
status = cfnresponse.FAILED
finally:
timer.cancel()
cfnresponse.send(event, context, status, {}, None)

lambda :

    from __future__ import print_function
import boto3
from boto3.dynamodb.conditions import Key, Attr
import json
dynamodb = boto3.resource('dynamodb')

def lambda_handler(event, context):
#print(event)
PhoneNumber=event['Details']['ContactData']['Attributes'].get('PhoneNumber',"Not Available")
table = dynamodb.Table('ClientOrders')
try:
response = table.query(
KeyConditionExpression=Key('PhoneNumber').eq(PhoneNumber)
)
except ClientError as e:
print(e.response['Error']['Message'])
return {
"Success":"False",
"Reason":e.response['Error']['Message']
}
else:
#item = response['Item']
print("GetItem succeeded:")
print(json.dumps(response))
print(response['Items'])
if "Items" in response:
return{
"Success":"True",
"PhoneNumber":response["Items"][0]["PhoneNumber"]
}
else:
return {
"Success":"False",
"Reason":"No Records Found"
}

提前致谢!

最佳答案

我解决了,你需要使用ZipFile并保持CF模板的格式。我正在通过 CF 设计器构建器检查是否有效,但我建议无论如何都尝试运行它,并在出现错误时对其进行分析。

关于python - 需要将 lambda 函数(python)加入 YAML 中的 CloudFormation 模板,我怎样才能实现这一点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66764798/

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