首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在CloudFormation中创建变量计数资源?

如何在CloudFormation中创建变量计数资源?
EN

Stack Overflow用户
提问于 2020-08-11 21:37:14
回答 1查看 525关注 0票数 0

我需要创建变量计数S3存储桶,其名称来自参数。例如,我有一个带有S3存储桶名称的数组。def bucketNames =“第一桶”,“第二桶”,...,“n桶”。有可能做到吗?也许可以使用嵌套堆栈。

EN

回答 1

Stack Overflow用户

发布于 2020-08-17 02:50:07

您可以使用自定义资源执行此操作:

此模板将创建自定义资源:

代码语言:javascript
复制
    AWSTemplateFormatVersion: '2010-09-09'
    Description: create buckets from parameters
    Parameters:
      BucketList:
        Description: comma delimited list of bucket names
        Type: CommaDelimitedList
        Default: athos,porthos,aramis
    Resources:
      BucketCreatorLambda:
        Type: AWS::Lambda::Function
        Properties:
          Handler: index.handler
          Code: ./bucketor/
          Runtime: python3.8
          Role: !GetAtt 'LambdaExecutionRole.Arn'
      CustomBucketCreator:
        Type: AWS::CloudFormation::CustomResource
        Properties:
          loglevel: info
          Buckets: !Ref 'BucketList'
          RoleArn: !GetAtt 'LambdaExecutionRole.Arn'
          ServiceToken: !GetAtt 'BucketCreatorLambda.Arn'
      LambdaExecutionRole:
        Type: AWS::IAM::Role
        Properties:
          AssumeRolePolicyDocument:
            Version: '2012-10-17'
            Statement:
              - Effect: Allow
                Principal:
                  Service:
                    - lambda.amazonaws.com
                Action:
                  - sts:AssumeRole
          ManagedPolicyArns:
            - !Sub 'arn:${AWS::Partition}:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole'
          Path: /
          Policies:
            - PolicyName: BucketPolicy
              PolicyDocument:
                Version: '2012-10-17'
                Statement:
                  - Effect: Allow
                    Action:
                      - s3:CreateBucket
                    Resource: arn:aws:s3:::*
    Outputs:
      BucketsCreated:
        Description: names of buckets created
        Value: !GetAtt 'CustomBucketCreator.BucketNames'

以及lambda的python脚本:

代码语言:javascript
复制
    # python file named index.py located ./bucketor/ with respect to cloudformation file
    from crhelper import CfnResource
    import logging
    import boto3
    
    logger = logging.getLogger(__name__)
    # Initialise the helper, all inputs are optional, this example shows the defaults
    helper = CfnResource(json_logging=True, log_level='DEBUG',
                         boto_level='CRITICAL', sleep_on_delete=120)
    
    s3 = boto3.client("s3")
    
    try:
        # Init code goes here
        pass
    except Exception as e:
        logger.error(e, exc_info=True)
        helper.init_failure(e)
    
    
    @helper.create
    def create(event, context):
        bucket_names = []
        buckets = event["ResourceProperties"]["Buckets"]
        for bucket in buckets:
            bucket_names.append(s3.create_bucket(Bucket=bucket)["Bucket"])
        helper.Data.update({"BucketNames": ",".join(bucket_names)})
    
    
    def handler(event, context):
        global logger
        helper(event, context)

@helper.create将为您创建存储桶。您还需要编写适当的@helper.delete@helper.update装饰器,如果不这样做,则需要手动删除自定义资源

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63359362

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档