다른 AWS CloudFormation 태스크에서 사용되는 내보내기 변수를 업데이트하려고 할 때 오류 메시지가 나타납니다. 오류는 다음과 같습니다. ‘다른 스택에서 사용 중이므로 내보내기 변수를 업데이트할 수 없습니다.’
간략한 설명
이 오류를 해결하려면 AWS Systems Manager Parameter Store에서 SSM 파라미터를 사용하여 CloudFormation 스택 간에 값을 공유합니다. SSM 파라미터는 다른 스택(stackB)에서 사용할 수 있는 특정 스택(stackA)에 값을 저장합니다.
SSM 파라미터를 사용하는 경우 두 CloudFormation 스택 간에 종속성이 없습니다. AWS Systems Manager Parameter Store에 변수가 저장되기 때문입니다.
참고: CloudFormation은 여러 SSM 파라미터 유형을 지원합니다.
해결 방법
1. 다음 템플릿을 기반으로 CloudFormation 스택(예: stackA)을 생성합니다.
{
"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"
}
}
}
}
}
위의 템플릿은 인스턴스와 SSM 파라미터를 생성합니다. SSM 파라미터의 값은 생성된 인스턴스의 가용 영역으로 설정됩니다. 이제 stackA는 Name이 AvailabilityZone으로, Value가 us-east-2a로 각각 설정된 SSM Parameter Store를 생성합니다.
참고: SSM 파라미터에는 고유한 이름을 사용해야 합니다.
2. 다음 템플릿을 기반으로 다른 CloudFormation 스택(예: stackB)을 생성합니다.
{
"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" }
}
}
}
}
위의 템플릿은 1단계에서 stackA에 의해 생성된 SSM 파라미터를 사용합니다.
참고: 이 파라미터를 사용하려면 stackB에서 파라미터를 선언해야 합니다. Type을 **AWS::SSM::Parameter::Value<String>**으로 설정합니다. 그러면 stackB에서 생성된 인스턴스가 us-east-2a 가용 영역에서 시작됩니다.
3. stackA를 업데이트하려면 AvailabilityZone 파라미터를 us-east-2a에서 us-east-2b로 변경합니다.
4. stackB와 동일한 업데이트된 가용 영역의 stackA에서 인스턴스를 시작합니다.
이제 인스턴스가 대체되어 us-east-2b 가용 영역에서 시작됩니다.