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

3 minutos de lectura
0

I receive an error message when I try to update an export variable that’s used in another AWS CloudFormation stack. 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 can be used by another stack (stackB).

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

Note: CloudFormation supports several SSM parameter types.

Resolution

1.    Create a CloudFormation stack (for example, stackA) 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. Now 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 (for example, stackB) 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" }
      }
    }
  }
}

The preceding template uses the SSM parameter that was created by stackA from 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.

Now the instance is replaced and launched in the us-east-2b Availability Zone.


OFICIAL DE AWS
OFICIAL DE AWSActualizada hace 2 años