如果存在bucket,如何强制CloudFormation创建一个类似名称的新bucket



有人能帮我做一个模板吗?在这个模板中,cloudformation会自动创建一个类似名称的新bucket。例如,如果我的bucket的名称是myownbucket,那么cloudformation是否可以自动创建一个名为myownbucket1的新bucket(如果已经有一个名称为myownbucket的bucket(。感谢

要通过cloudformation在AWS资源管理中创建或更新现有的bucket,您需要在您的cfn.yaml/json.中拥有自定义资源

S3Checking:
Type: 'Custom::CheckS3Bucket'
Properties:
ServiceToken: !GetAtt CustomResourceS3Function.Arn
Bucket: !Ref NotifyBucket

CustomResourceS3Function将看起来像这个

CustomResourceLambdaFunction:
Type: 'AWS::Lambda::Function'
Properties:
Handler: index.lambda_handler
Role: !GetAtt LambdaIAMRole.Arn
Code:
ZipFile: |
from __future__ import print_function
import json
import boto3
import cfnresponse
SUCCESS = "SUCCESS"
FAILED = "FAILED"
print('Loading function')
s3 = boto3.resource('s3')
def lambda_handler(event, context):
print("Received event: " + json.dumps(event, indent=2))
responseData={}
try:
print("Request Type:",event['RequestType'])
bucket_name=event['ResourceProperties']['Bucket']
bucket=event['ResourceProperties']['Bucket']
update_delete_buckets(bucket_name)
responseData={'Bucket':Bucket}
print("Sending response to custom resource")
responseStatus = 'SUCCESS'
except Exception as e:
print('Failed to process:', e)
responseStatus = 'FAILED'
responseData = {'Failure': 'Something bad happened.'}
cfnresponse.send(event, context, responseStatus, responseData)
def update_delete_buckets(bucket):
bucket_obj = s3.Bucket(bucket)
if bucket_obj.creation_date:
print("The bucket exists")
else:
print("The bucket does not exist")
s3.create_bucket(Bucket=bucket)
Runtime: python3.9
Timeout: 50

最新更新