CloudFormationで作成したIAMリソース名、任意の名称が指定出来る様になりました

2016.08.11

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

はじめに

AWSチームのすずきです。

2016年7月のアップデートにより、CloudFormationで作成したIAMのユーザ、グループ、ロールに対して、任意のリソース名を付与する事が可能になりました。

AWS CloudFormation Adds Support for AWS IoT and Additional Updates

今回、その動作を試す機会がありましたので、紹介させて頂きます。

名称指定あり

  • 「Properties」として、「GroupName」, 「UserName」, 「RoleName」の指定を行う事で、任意の名称が反映されます。

cfn-iam-custom-name-01

サンプルテンプレート

{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "IAM name test 2016-07-20",
"Resources": {
"IamGroup01": {
"Type": "AWS::IAM::Group",
"Properties": {
"GroupName": "Group01"
}
},
"IamUser01": {
"Type": "AWS::IAM::User",
"Properties": {
"UserName": "User01"
}
},
"IamRole01": {
"Type": "AWS::IAM::Role",
"Properties": {
"AssumeRolePolicyDocument": {
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Principal": { "Service": [ "ec2.amazonaws.com" ]},
"Action": [ "sts:AssumeRole" ]
}]
},
"RoleName": "Role01"
}
}
}
}

名称指定なし

  • IAMリソース名を指定しない場合、従来と同様の仕様でユーザ、グループ、ロール名が決まります。
  • 命名仕様「<CloudFormationスタック名>-<CloudFormationリソース名>-<ランダム文字列>」となります。

cfn-iam-custom-name-02

サンプルテンプレート

{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "IAM test 2016-07-20",
"Resources": {
"IamGroup01": {
"Type": "AWS::IAM::Group"
},
"IamUser01": {
"Type": "AWS::IAM::User"
},
"IamRole01": {
"Type": "AWS::IAM::Role",
"Properties": {
"AssumeRolePolicyDocument": {
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Principal": { "Service": [ "ec2.amazonaws.com" ]},
"Action": [ "sts:AssumeRole" ]
}]
}
}
}
}
}

注意点

  • IAMユーザ、グループ、ロールは、利用中のAWSアカウント内で一意である事が求められます。IAMのリソース名指定をした場合、CloudFormation操作時に確認が求められます。

cfn-iam-custom-name-03

  • 指定したリソース名が重複した場合、CloudFormation操作は失敗します。

cfn-iam-custom-name-04

  • 1つのCloudFormationテンプレートを利用し複数の環境を構築する場合、リソース名の重複回避措置が必要です。

リソース名の重複回避例

{
"AWSTemplateFormatVersion": "2010-09-09",
"Parameters": {
"Environment": {
"Type": "String",
"Default": "production",
"AllowedValues": [
"staging",
"production"
]
}
},
"Resources": {
"IamUser01": {
"Type": "AWS::IAM::User",
"Properties": {
"UserName": { "Fn::Join": [ "-", [ "User01", { "Ref": "Environment" } ]]}
}
}
}
}

まとめ

今回のアップデートにより、AWSアカウントを跨ぐロールやポリシーの管理にCloudFormationを利用しやすくなりました。 RedshiftやLambdaや、監視やCIなどのSaaSなどへのIAM権限を用意する際、その利用をご検討頂ければと思います。