Skip to content

Commit 8f20597

Browse files
committed
feat: support China region
1 parent 1f473a0 commit 8f20597

File tree

6 files changed

+47
-31
lines changed

6 files changed

+47
-31
lines changed

builder/common/helper_funcs.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,10 @@ func DestroyAMIs(imageids []*string, ec2conn *ec2.EC2) error {
7676
}
7777
return nil
7878
}
79+
80+
func AwsPartition(isRestricted bool) string {
81+
if isRestricted {
82+
return "aws-cn"
83+
}
84+
return "aws"
85+
}

builder/common/step_iam_instance_profile.go

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
)
1919

2020
const (
21-
AmazonSSMManagedInstanceCorePolicyArn = "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"
21+
AmazonSSMManagedInstanceCorePolicyArnPart = "iam::aws:policy/AmazonSSMManagedInstanceCore"
2222
)
2323

2424
type 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
}

builder/ebs/builder.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -340,10 +340,11 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
340340
Ctx: b.config.ctx,
341341
},
342342
&awscommon.StepIamInstanceProfile{
343-
PollingConfig: b.config.PollingConfig,
344-
IamInstanceProfile: b.config.IamInstanceProfile,
345-
SkipProfileValidation: b.config.SkipProfileValidation,
346-
SSMAgentEnabled: b.config.SSMAgentEnabled(),
343+
PollingConfig: b.config.PollingConfig,
344+
IamInstanceProfile: b.config.IamInstanceProfile,
345+
SkipProfileValidation: b.config.SkipProfileValidation,
346+
SSMAgentEnabled: b.config.SSMAgentEnabled(),
347+
IsRestricted: b.config.IsChinaCloud(),
347348
TemporaryIamInstanceProfilePolicyDocument: b.config.TemporaryIamInstanceProfilePolicyDocument,
348349
Tags: b.config.RunTags,
349350
Ctx: b.config.ctx,

builder/ebssurrogate/builder.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -424,10 +424,11 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
424424
Ctx: b.config.ctx,
425425
},
426426
&awscommon.StepIamInstanceProfile{
427-
PollingConfig: b.config.PollingConfig,
428-
IamInstanceProfile: b.config.IamInstanceProfile,
429-
SkipProfileValidation: b.config.SkipProfileValidation,
430-
SSMAgentEnabled: b.config.SSMAgentEnabled(),
427+
PollingConfig: b.config.PollingConfig,
428+
IamInstanceProfile: b.config.IamInstanceProfile,
429+
SkipProfileValidation: b.config.SkipProfileValidation,
430+
SSMAgentEnabled: b.config.SSMAgentEnabled(),
431+
IsRestricted: b.config.IsChinaCloud(),
431432
TemporaryIamInstanceProfilePolicyDocument: b.config.TemporaryIamInstanceProfilePolicyDocument,
432433
},
433434
&awscommon.StepCleanupVolumes{

builder/ebsvolume/builder.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -312,10 +312,11 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
312312
Ctx: b.config.ctx,
313313
},
314314
&awscommon.StepIamInstanceProfile{
315-
PollingConfig: b.config.PollingConfig,
316-
IamInstanceProfile: b.config.IamInstanceProfile,
317-
SkipProfileValidation: b.config.SkipProfileValidation,
318-
SSMAgentEnabled: b.config.SSMAgentEnabled(),
315+
PollingConfig: b.config.PollingConfig,
316+
IamInstanceProfile: b.config.IamInstanceProfile,
317+
SkipProfileValidation: b.config.SkipProfileValidation,
318+
SSMAgentEnabled: b.config.SSMAgentEnabled(),
319+
IsRestricted: b.config.IsChinaCloud(),
319320
TemporaryIamInstanceProfilePolicyDocument: b.config.TemporaryIamInstanceProfilePolicyDocument,
320321
},
321322
instanceStep,

builder/instance/builder.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -383,10 +383,11 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
383383
Ctx: b.config.ctx,
384384
},
385385
&awscommon.StepIamInstanceProfile{
386-
PollingConfig: b.config.PollingConfig,
387-
IamInstanceProfile: b.config.IamInstanceProfile,
388-
SkipProfileValidation: b.config.SkipProfileValidation,
389-
SSMAgentEnabled: b.config.SSMAgentEnabled(),
386+
PollingConfig: b.config.PollingConfig,
387+
IamInstanceProfile: b.config.IamInstanceProfile,
388+
SkipProfileValidation: b.config.SkipProfileValidation,
389+
SSMAgentEnabled: b.config.SSMAgentEnabled(),
390+
IsRestricted: b.config.IsChinaCloud(),
390391
TemporaryIamInstanceProfilePolicyDocument: b.config.TemporaryIamInstanceProfilePolicyDocument,
391392
},
392393
instanceStep,

0 commit comments

Comments
 (0)