How can I use a value from an SSM StringList in a CloudFormation template?

2.4k views Asked by At

Let's assume I have a parameter in the SSM ParameterStore. The parameter has a StringList as value and describes a service, such as (bucket_name, request_url)

"serviceA" = "bucket_name_A, https://www.request.com/A"

Now, in CloudFormation I want to define the name of my bucket from this stringlist.

"S3FTSE100Intraday1min": {
            "Type": "AWS::S3::Bucket",
            "Properties": {
                "BucketName": {
                    "Fn::GetAtt": [
                        "My_SSM_ParameterStore_Logical_ID",
                        "Value"
                    ]
                },
                ...

But obviously this will return the full stringlist, not just bucket_name_A

How can I access one of the parameters in the stringlist to be used in the CloudFormation template?

1

There are 1 answers

0
Erken On BEST ANSWER

Whilst drafting this question I started looking into the Fn:: methods

(source: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-split.html)

and I got an idea:

The stringlist is: "bucket_name_A, https://www.request.com/A"

  • retrieve the stringlist value with Fn::GetAtt => "bucket_name_A, https://www.request.com/A"
  • split it into a list with Fn::Split => ["bucket_name_A", "https://www.request.com/A"]
  • select the first value with Fn::Select => "bucket_name_A"

and it works! Here below is the cloudformation template:

SSM Parameter Store:

"SSMTestBucketName": {
            "Type": "AWS::SSM::Parameter",
            "Properties": {
                "Name": "StringList_Test_Bucket_Name",
                "Type": "StringList",
                "Value": "test-ssm-stringlist-bucket, https://www.requesturl.com"
            }
        }

S3 Bucket:

"S3TestBucket": {
            "Type": "AWS::S3::Bucket",
            "Properties": {
                "BucketName": {
                    "Fn::Select": [
                        "0",
                        {
                            "Fn::Split": [
                                ",",
                                {
                                    "Fn::GetAtt": [
                                        "SSMTestBucketName",
                                        "Value"
                                    ]
                                }
                            ]
                        }
                    ]
                },
                "BucketEncryption": {
                    "ServerSideEncryptionConfiguration": [
                        {
                            "ServerSideEncryptionByDefault": {
                                "SSEAlgorithm": "AES256"
                            }
                        }
                    ]
                },
                "PublicAccessBlockConfiguration": {
                    "BlockPublicAcls": true,
                    "BlockPublicPolicy": true,
                    "IgnorePublicAcls": true,
                    "RestrictPublicBuckets": true
                },
                "LifecycleConfiguration": {
                    "Rules": [
                        {
                            "Status": "Enabled",
                            "Transitions": [
                                {
                                    "TransitionInDays": "30",
                                    "StorageClass": "STANDARD_IA"
                                }
                            ]
                        }
                    ]
                }
            }
        }