AWS Cloudformation- How to do string Uppercase or lowercase in json/yaml template

后端 未结 4 870
北恋
北恋 2021-02-19 11:18

I am working on AWS CloudFormation and I created one template in which I asked user to select Environment.

On the basis of selected value I created the resources.

4条回答
  •  暖寄归人
    2021-02-19 12:03

    The accepted answer suggested using a CloudFormation macro, and another answer suggesting using FindInMap.

    FindInMap is not very useful here, since it would only work with hardcoded values.

    The macro suggestion will work, but requires quite a bit of setup (declare the macro in a separate stack, ensure your deployer role has permission to invoke the Lambda, and your CloudFormation stack is deployed with CAPABILITY_AUTO_EXPAND, and so on).

    Declaring a custom resource within the template will work and IMO involves less work than relying on the macro. Here's a CFN snippet, adapting the S3 bucket resource you were asking about, demonstrating the use of a custom resource which will lowercase an arbitrary S3 bucket name:

      # Custom resource to transform input to lowercase.                                             
      LowerCaseLambda:
        Type: 'AWS::Lambda::Function'
        Properties:
          Description: Returns the lowercase version of a string
          MemorySize: 256
          Runtime: python3.8
          Handler: index.lambda_handler
          Role: !GetAtt LowerCaseLambdaRole.Arn
          Timeout: 30
          Code:
            ZipFile: |
              import cfnresponse
    
              def lambda_handler(event, context):                                                    
                  output = event['ResourceProperties'].get('InputString', '').lower()                
                  responseData = {'OutputString': output}                                            
                  cfnresponse.send(event, context, cfnresponse.SUCCESS, responseData)                
    
      LowerCaseLambdaRole:
        Type: AWS::IAM::Role
        Properties:
          AssumeRolePolicyDocument:
            Version: "2012-10-17"
            Statement:
              - Effect: "Allow"
                Principal:
                  Service:
                    - "lambda.amazonaws.com"
                Action:
                  - "sts:AssumeRole"
          Policies:
            - PolicyName: "lambda-write-logs"
              PolicyDocument:
                Version: "2012-10-17"
                Statement:
                  - Effect: "Allow"
                    Action:
                      - "logs:CreateLogGroup"
                      - "logs:CreateLogStream"
                      - "logs:PutLogEvents"
                    Resource: "arn:aws:logs:*:*"
    
    
      S3BucketName:
        Type: Custom::Lowercase
        Properties:
          ServiceToken: !GetAtt LowerCaseLambda.Arn
          InputString: !Ref selectedEnv
    
      S3Bucket:
        BucketName: !Join
          - ''
          - - !GetAtt S3BucketName.OutputString
            - "-deployment.companyname.com"
    

提交回复
热议问题