我想要一个使用 CloudFormation 创建一个新的自定义资源来获得当前日期减去 X 天的结果,当我创建 lambda 函数时,我得到错误:
资源未能在预期时间内稳定下来。
我的代码是:
AWSTemplateFormatVersion: 2010-09-09
Parameters:
Environment:
Description: Environment
Type: String
Default: dev
AllowedValues:
- dev
- test
- prod
- sbx
DaysToSubstract:
Description: Days To Substract to calculate dates to ingest with RedshiftLoader
Type: Number
Default: 1
Resources:
lambdaDateRedshiftLoader:
Type: 'AWS::Lambda::Function'
DependsOn:
- lambdaDateRedshiftLoaderRole
Properties:
Code:
ZipFile: !Sub |
from datetime import date, timedelta
import cfnresponse
def lambda_handler(event, context):
current_delta = date.today() - timedelta(days=event['DaysToSubstract'])
current_delta_str = current_delta.strftime("%Y-%m-%d")
responseData['Dates'] = current_delta_str
cfnresponse.send(event, context, cfnresponse.SUCCESS, responseData)
Environment:
Variables:
DaysToSubstract: !Sub '${DaysToSubstract}'
Description: >-
Calculate yesterday date to obtain start and end date to load the data
with RedshiftLoader
Handler: index.lambda_handler
MemorySize: 128
Role: !GetAtt lambdaDateRedshiftLoaderRole.Arn
Runtime: python3.7
Timeout: 30
lambdaDateRedshiftLoaderRole:
Type: 'AWS::IAM::Role'
Properties:
RoleName: !Sub 'a3m${Environment}-datesRL-lambda-role'
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Action:
- 'sts:AssumeRole'
Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Path: /service-role/
Policies:
- PolicyName: !Sub 'a3m${Environment}-lambda-datesRL-lambda-logs-policy'
PolicyDocument:
Version: 2012-10-17
Statement:
- Action:
- 'logs:CreateLogGroup'
- 'logs:CreateLogStream'
- 'logs:PutLogEvents'
Effect: Allow
Resource:
- !Sub >-
arn:aws:logs:eu-west-1:${AWS::AccountId}:log-group:/aws/lambda/lambda-datesRL-uyc:*
lambdaRL:
Type: 'Custom::Value'
Properties:
ServiceToken: !GetAtt lambdaDateRedshiftLoader.Arn
Outputs:
LambdaFunctionOutput:
Value: !GetAtt lambdaRL.Dates
Description: Return Value of Lambda Function (Date minus x days)
我在输出中使用cfnresponse
和SUCCESS
使用!GetAtt lambdaRL.Dates
。
先感谢您