gpt4 book ai didi

aws-lambda - Lambda 函数完成后 Amazon Cloudformation 堆栈挂起

转载 作者:行者123 更新时间:2023-12-03 07:31:00 24 4
gpt4 key购买 nike

我有一个简单的模板来创建 S3 存储桶,然后调用 Lambda 函数将对象从公共(public)存储桶复制到其中:

AWSTemplateFormatVersion: 2010-09-09
Transform: AWS::Serverless-2016-10-31
Description: Create an S3 Bucket, populate it with a copy of a sample CSV file
Parameters:
bucketname:
Type: String
Description: Name of bucket where the CSV file will be bootstrapped, bucket names MUST be unique across AWS and no Upper Case characters - 8 or more characters
MinLength: 8
Outputs:
bucketURL:
Value:
Fn::Join:
- ''
- - 'https://s3.amazonaws.com/'
- !Ref bucketname
Resources:
S3Bucket:
Type: AWS::S3::Bucket
DeletionPolicy: Retain
Properties:
BucketName: !Ref bucketname
deploytos3:
Type: AWS::Serverless::Function
Properties:
Handler: index.handler
Runtime: nodejs12.x
CodeUri: 's3://mongodb-aws-jam/lambdaDeploy.zip'
Policies:
- AWSLambdaBasicExecutionRole
- AmazonS3FullAccess
DeploymentCustomResource:
Type: Custom::deploytos3
Properties:
ServiceToken: !GetAtt deploytos3.Arn
bucketname: !Ref bucketname

这是 Lambda 函数:

const AWS = require('aws-sdk');
var response = require('cfn-response');

const s3 = new AWS.S3();
const srcBucket = "mongodb-aws-jam"
const srcKey = "SampleData.csv"

exports.handler = async (event, context) => {
console.log("start")
myBucket = event.ResourceProperties['bucketname']
console.log(`Will bootstrap ${srcKey} object in ${myBucket} bucket`)

const copyparams = {
Bucket : myBucket,
CopySource : `/${srcBucket}/${srcKey}`,
Key : srcKey
};

try {
await s3.copyObject(copyparams).promise();
} catch (error) {
const errorText = `Failed to copy file to ${myBucket}/${srcKey}: error`
console.log(errorText)
// callback(null, {statusCode: 500, body: errorText})
response.send(event, context, response.FAILED, {Error: errorText})
return
}
resultText = `Copied file to ${myBucket}/${srcKey}`
console.log(resultText)
// callback(null, {statusCode: 200, body: resultText})
response.send(event, context, response.SUCCESS, {})
};

堆栈成功创建并引导新存储桶,但堆栈历史记录显示 DeploymentCustomResource 资源从未超过 CREATE_IN_PROGRESS 状态,即使 lambda 日志显示该功能成功完成。我尝试了使用回调的各种替代方法,并将函数减少到不执行任何工作,但堆栈总是挂起。

有什么建议吗?

最佳答案

由于 CloudFormation (CFN) 等待函数的正确响应,您的函数陷入 CREATE_IN_PROGRESS 状态。您的代码没有提供它。

自定义资源的函数需要特殊设计。必须正确处理CFN事件,并做出相应的响应。

对于nodejs中的函数,您可以使用辅助库 cfn-response,如cfn-response module所示。 Nodejs 中自定义资源函数的示例为 here .

关于aws-lambda - Lambda 函数完成后 Amazon Cloudformation 堆栈挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64315876/

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