@@ -18,7 +18,7 @@ import (
1818)
1919
2020const (
21- AmazonSSMManagedInstanceCorePolicyArn = "arn:aws: iam::aws:policy/AmazonSSMManagedInstanceCore"
21+ AmazonSSMManagedInstanceCorePolicyArnPart = "iam::aws:policy/AmazonSSMManagedInstanceCore"
2222)
2323
2424type StepIamInstanceProfile struct {
@@ -27,6 +27,7 @@ type StepIamInstanceProfile struct {
2727 SkipProfileValidation bool
2828 TemporaryIamInstanceProfilePolicyDocument * PolicyDocument
2929 SSMAgentEnabled bool
30+ IsRestricted bool
3031 createdInstanceProfileName string
3132 createdRoleName string
3233 createdPolicyName string
@@ -81,18 +82,22 @@ func (s *StepIamInstanceProfile) Run(ctx context.Context, state multistep.StateB
8182 }
8283
8384 ui .Sayf ("Creating temporary role for this instance: %s" , profileName )
84- trustPolicy := `{
85- "Version": "2012-10-17",
86- "Statement": [
87- {
88- "Effect": "Allow",
89- "Principal": {
90- "Service": "ec2.amazonaws.com"
91- },
92- "Action": "sts:AssumeRole"
93- }
94- ]
95- }`
85+ service := "ec2.amazonaws.com"
86+ if s .IsRestricted {
87+ service = "ec2.amazonaws.com.cn"
88+ }
89+ trustPolicy := fmt .Sprintf (`{
90+ "Version": "2012-10-17",
91+ "Statement": [
92+ {
93+ "Effect": "Allow",
94+ "Principal": {
95+ "Service": "%s"
96+ },
97+ "Action": "sts:AssumeRole"
98+ }
99+ ]
100+ }` , service )
96101 roleResp , err := iamsvc .CreateRole (& iam.CreateRoleInput {
97102 RoleName : aws .String (profileName ),
98103 Description : aws .String ("Temporary role for Packer" ),
@@ -136,7 +141,7 @@ func (s *StepIamInstanceProfile) Run(ctx context.Context, state multistep.StateB
136141 s .createdPolicyName = profileName
137142 }
138143 if s .SSMAgentEnabled {
139- ssmPolicyArn := aws .String (AmazonSSMManagedInstanceCorePolicyArn )
144+ ssmPolicyArn := aws .String (fmt . Sprintf ( "arn:%s:%s" , AwsPartition ( s . IsRestricted ), AmazonSSMManagedInstanceCorePolicyArnPart ) )
140145 _ , err = iamsvc .AttachRolePolicy (& iam.AttachRolePolicyInput {
141146 PolicyArn : ssmPolicyArn ,
142147 RoleName : aws .String (s .createdRoleName ),
@@ -204,7 +209,7 @@ func (s *StepIamInstanceProfile) Cleanup(state multistep.StateBag) {
204209
205210 if s .SSMAgentEnabled {
206211 iamsvc .DetachRolePolicy (& iam.DetachRolePolicyInput {
207- PolicyArn : aws .String (AmazonSSMManagedInstanceCorePolicyArn ),
212+ PolicyArn : aws .String (fmt . Sprintf ( "arn:%s:%s" , AwsPartition ( s . IsRestricted ), AmazonSSMManagedInstanceCorePolicyArnPart ) ),
208213 RoleName : aws .String (s .createdRoleName ),
209214 })
210215 }
0 commit comments