How do I use parameters in AWS Systems Manager Parameter Store to share values between CloudFormation stacks?

3 minute read
0

When I try to update an export variable that’s used in another AWS CloudFormation stack, I receive an error message. The error is: "Cannot update an export variable as it is in use by another stack."

Short description

To resolve this error, use SSM parameters in AWS Systems Manager Parameter Store to share values between CloudFormation stacks. An SSM parameter stores a value in one stack (stackA) that another stack (stackB) can use.

When you use an SSM parameter, there isn't a dependency between the two CloudFormation stacks. This is because the AWS Systems Manager Parameter Store stores the variable.

Note: CloudFormation supports several SSM parameter types.

Resolution

1.    Create a CloudFormation stack (stackA) that's based on the following template:

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Parameters": {
    "AvailabilityZone": {
      "Description": "Amazon EC2 instance Availability Zone",
      "Type": "String"
    },
    "AMIId": {
      "Description": "The Amazon Machine Image id",
      "Type": "String"
    },
    "InstanceType": {
      "Description": "The Type of instance",
      "Type": "String"
    }
  },
  "Resources": {
    "myinstance": {
      "Type": "AWS::EC2::Instance",
      "Properties": {
        "AvailabilityZone": {
          "Fn::GetAtt": ["BasicParameter", "Value"]
        },
        "ImageId": {
          "Ref": "AMIId"
        },
        "InstanceType": {
          "Ref": "InstanceType"
        }
      }
    },
    "BasicParameter": {
      "Type": "AWS::SSM::Parameter",
      "Properties": {
        "Name": "AvailabilityZone",
        "Type": "String",
        "Value": {
          "Ref": "AvailabilityZone"
        }
      }
    }
  }
}

The preceding template creates an instance and an SSM parameter. The value of the SSM parameter is set to the Availability Zone of the instance that's created. stackA creates an SSM Parameter Store with Name set to AvailabilityZone and Value set to us-east-2a.

Note: You must use a unique name for your SSM parameter.

2.    Create another CloudFormation stack (stackB) that's based on the following template.

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Parameters": {
    "AMIId": {
      "Description": "The Amazon Machine Image id",
      "Type": "String"
    },
    "InstanceType": {
      "Description": "The Type of instance",
      "Type": "String"
    },
    "AvailabilityZone": {
      "Description": "Amazon EC2 instance Availablity Zone",
      "Type": "AWS::SSM::Parameter::Value<String>",
      "Default": "AvailabilityZone"
    }
  },
  "Mappings": {},
  "Conditions": {},
  "Resources": {
    "myinstance": {
      "Type": "AWS::EC2::Instance",
      "Properties": {
        "AvailabilityZone": {
          "Ref": "AvailabilityZone"
        },
        "ImageId": {
          "Ref": "AMIId"
        },
        "InstanceType": {
          "Ref": "InstanceType"
        }
      }
    }
  }
}

This template uses the SSM parameter that stackA created in step 1.

Note: To use this parameter, you must declare a parameter in stackB. Set Type to AWS::SSM::Parameter::Value<String>. The instance created from stackB is then launched in the us-east-2a Availability Zone.

3.    To update stackA, change the AvailabilityZone parameter from us-east-2a to us-east-2b.

4.    Launch the instance from stackA in the same updated Availability Zone as stackB. The instance is replaced and then launched in the us-east-2b Availability Zone.

AWS OFFICIAL
AWS OFFICIALUpdated 8 months ago