CloudFormation StackSetsを使い、複数のリージョンにスタックをデプロイしてみた

2021.12.20

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

こんにちは、イムチェジョンです。
CloudFormation StackSetsを使いと、簡単にいろんなアカウントやリージョンにCloudFormationのスタックを配布することができます。
なので、今回は実際にCloudFormation StackSetsを使ってまとめてみます。

アジェンダ

  1. CloudFormation StackSetsとは?
  2. StackSetsで複数のリージョンにデプロイ
  3. まとめ

1. CloudFormation StackSetsとは?

  • スタックセットを使用すると、AWS CloudFormationテンプレートを使用して複数リージョン、複数のAWSアカウントにスタックを作成
  • 各スタックに含まれたすべてのリソースはスタックセットのAWS CloudFormationテンプレートによって定義される
  • スタックセットはリージョンリソースです。 1リージョンにスタックセットを作成すると、他のリージョンでそのスタックセットを見たり変更することができません。

2. StackSetsで複数のリージョンにデプロイ

CloudFormation テンプレート

CloudFormation StackSetsを試してみるため、ネットワーク環境を構築するテンプレートを作成します。

AWSTemplateFormatVersion: "2010-09-09"
Description: Network of testing StackSets
  
# ------------
# Parameters
# ------------
Parameters:
	Name:
		Default: "StackSets"
		Type: String
		Description: "Stack name"
	VpcCidr:
		Type: String
		AllowedPattern: '(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/(\d{1,2})'
		ConstraintDescription: must be a valid IP CIDR range of the form x.x.x.x/x.
		Description: "VPC CIDR range"
  
# ------------
# Resources
# ------------
Resources:
	vpc:
		Type: AWS::EC2::VPC
		Properties:
			CidrBlock: !Ref  VpcCidr
			EnableDnsHostnames: true
			EnableDnsSupport: true
			Tags:
				- Key: Name
				  Value: !Sub  ${Name}-vpc
  
	# -----
	# Internet Gateway
	# -----
	InternetGateway:
		Type: AWS::EC2::InternetGateway
		Properties:
			Tags:
				- Key: Name
				  Value: !Sub  ${Name}-igw
	  
	VPCGatewayAttach:
		Type: AWS::EC2::VPCGatewayAttachment
		Properties:
			InternetGatewayId: !Ref  InternetGateway
			VpcId: !Ref  vpc
	  
	# -----
	# Public Subnet
	# -----
	publicSubnet:
		Type: AWS::EC2::Subnet
		Properties:
			AvailabilityZone: !Select [ 0, !GetAZs  '' ]
			CidrBlock: !Select [0, !Cidr [!GetAtt  vpc.CidrBlock, 2, 8]]
			VpcId: !Ref  vpc
			Tags:
				- Key: Name
				  Value: !Sub  ${Name}-public-subnet
	  
	# -----
	# Private Subnet
	# -----
	privateSubnet:
		Type: AWS::EC2::Subnet
		Properties:
			AvailabilityZone: !Select [ 1, !GetAZs  '' ]
			CidrBlock: !Select [10, !Cidr [!GetAtt  vpc.CidrBlock, 12, 8]]
			VpcId: !Ref  vpc
			Tags:
				- Key: Name
				  Value: !Sub  ${Name}-dmz-subnet
	  
	# -----
	# Route table public
	# -----
	PublicRouteTable:
		Type: AWS::EC2::RouteTable
		Properties:
			VpcId: !Ref  vpc
			Tags:
				- Key: Name
				  Value: !Sub  ${Name}-public-rtb
	  
	PublicRoute:
		Type: AWS::EC2::Route
		Properties:
			RouteTableId: !Ref  PublicRouteTable
			DestinationCidrBlock: 0.0.0.0/0
			GatewayId: !Ref  InternetGateway

	#----
	# Subnet to attach
	#----
	PublicSubnetRouteTableAssociation:
		Type: AWS::EC2::SubnetRouteTableAssociation
		Properties:
			SubnetId: !Ref  publicSubnet
			RouteTableId: !Ref  PublicRouteTable

実際にStackSets使用

IAMの下準備

CloudFormation StackSetsを使用するためには2つのIAMロールが必要です。
IAMロールはCloudFormationのテンプレートが作成されているのでCloudFormationで作成します。

  • StackSets作成する管理者アカウントのロール名
    • AWSCloudFormationStackSetAdministrationRole
  • Stackが作成されるアカウントのロール名
    • AWSCloudFormationStackSetExecutionRole

AWSCloudFormationStackSetAdministrationRole

AWSTemplateFormatVersion: 2010-09-09
Description: Configure the AWSCloudFormationStackSetAdministrationRole to enable use of AWS CloudFormation StackSets.

Resources:
	AdministrationRole:
		Type: AWS::IAM::Role
		Properties:
			RoleName: AWSCloudFormationStackSetAdministrationRole
			AssumeRolePolicyDocument:
				Version: 2012-10-17
				Statement:
					- Effect: Allow
						Principal:
							Service: cloudformation.amazonaws.com
						Action:
							- sts:AssumeRole
		Path: /
		Policies:
			- PolicyName: AssumeRole-AWSCloudFormationStackSetExecutionRole
				PolicyDocument:
					Version: 2012-10-17
					Statement:
						- Effect: Allow
							Action:
								- sts:AssumeRole
							Resource:
								- "arn:*:iam::*:role/AWSCloudFormationStackSetExecutionRole"

AWSCloudFormationStackSetExecutionRole

AWSTemplateFormatVersion: 2010-09-09
Description: Configure the AWSCloudFormationStackSetExecutionRole to enable use of your account as a target account in AWS CloudFormation StackSets.

Parameters:
	AdministratorAccountId:
		Type: String
		Description: AWS Account Id of the administrator account (the account in which StackSets will be created).
		MaxLength: 12
		MinLength: 12

Resources:
	ExecutionRole:
		Type: AWS::IAM::Role
		Properties:
			RoleName: AWSCloudFormationStackSetExecutionRole
			AssumeRolePolicyDocument:
				Version: 2012-10-17
				Statement:
					- Effect: Allow
						Principal:
							AWS:
								- !Ref  AdministratorAccountId
						Action:
							- sts:AssumeRole
			Path: /
			ManagedPolicyArns:
				- !Sub  arn:${AWS::Partition}:iam::aws:policy/AdministratorAccess

StackSets 作成

今回は3つのリージョンを対象にStackSetsを作成してみます。

CloudFormationのページで[StackSets]に入り、新しいStackSetsを作成します。

今回は同じアカウントの複数のリージョンを対象に作成するので、
IAM 管理ロール ARN : AWSCloudFormationStackSetAdministrationRole
IAM 実行ロール名 : AWSCloudFormationStackSetExecutionRole
を指定します。
また、作成したテンプレートファイルのアップロードします。

StackSet 名 : StackSets-vpc

VPCのサイダーも入力します。
VpcCidr : 10.0.0.0/16

スタックセットを追加するアカウントとリージョンを入力、選択します。

成功的にStackSetsが作成されてオペレーションのステータスがRUNNINGになっているのが確認できます。

スタックインスタンスで確認すると、3つのスタックが生成されているのも確認できます。

少し待つと、3つのリージョンでのスタックが作成完了され、オペレーションのステータスもSUCCEEDEDになります。

これでStackSetsの作成完了です。

StackSetsの確認

StackSetsがよく作成されたのを確認してみましょう。

東京リージョンのスタック

ソウルリージョンのスタック

シンガポールリージョンのスタック

スタックリソースもよく作成できました。