Home > OS >  Can you pass a parameter into a bash script in Cloudformation?
Can you pass a parameter into a bash script in Cloudformation?

Time:12-15

I'm trying to create a bash script to update EC2 instances to the latest version of Boto3. To do so I need to place the .zip file into a specified S3 bucket. I want the user to enter the name of their S3 bucket when the stack is being deployed and then have that entered into the bash script. Here is my code:

Parameters:
  BucketName:
    Type: String

Resources:  
  rRunLambdaLayerScript:
    Type: AWS::SSM::Association
    Properties:
      Name: AWS-RunShellScript
      WaitForSuccessTimeoutSeconds: 300
      Targets:
      - Key: tag:Lambda
        Values:
        - layer
      Parameters:
        commands:
            - |
                #!/usr/bin/env bash

                mkdir lambda_layer
                cd lambda_layer/
                mkdir python
                cd python/
                pip3 install boto3 -t ./
                cd ..
                zip -r /tmp/lambda_layer.zip .
                aws s3 cp /tmp/lambda_layer.zip s3://$BucketName

Doing it this way results in the script leaving it blank with a "parameter validation failed" error.

CodePudding user response:

You usually use Sub for that:

Parameters:
  BucketName:
    Type: String

Resources:  
  rRunLambdaLayerScript:
    Type: AWS::SSM::Association
    Properties:
      Name: AWS-RunShellScript
      WaitForSuccessTimeoutSeconds: 300
      Targets:
      - Key: tag:Lambda
        Values:
        - layer
      Parameters:
        commands:
            - !Sub |
                #!/usr/bin/env bash

                mkdir lambda_layer
                cd lambda_layer/
                mkdir python
                cd python/
                pip3 install boto3 -t ./
                cd ..
                zip -r /tmp/lambda_layer.zip .
                aws s3 cp /tmp/lambda_layer.zip s3://${BucketName}
  • Related