Skip to content

Commit 391ea21

Browse files
committed
feat: support China region
1 parent 664977a commit 391ea21

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
@@ -345,10 +345,11 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
345345
Ctx: b.config.ctx,
346346
},
347347
&awscommon.StepIamInstanceProfile{
348-
PollingConfig: b.config.PollingConfig,
349-
IamInstanceProfile: b.config.IamInstanceProfile,
350-
SkipProfileValidation: b.config.SkipProfileValidation,
351-
SSMAgentEnabled: b.config.SSMAgentEnabled(),
348+
PollingConfig: b.config.PollingConfig,
349+
IamInstanceProfile: b.config.IamInstanceProfile,
350+
SkipProfileValidation: b.config.SkipProfileValidation,
351+
SSMAgentEnabled: b.config.SSMAgentEnabled(),
352+
IsRestricted: b.config.IsChinaCloud(),
352353
TemporaryIamInstanceProfilePolicyDocument: b.config.TemporaryIamInstanceProfilePolicyDocument,
353354
Tags: b.config.RunTags,
354355
Ctx: b.config.ctx,

builder/ebssurrogate/builder.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -429,10 +429,11 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
429429
Ctx: b.config.ctx,
430430
},
431431
&awscommon.StepIamInstanceProfile{
432-
PollingConfig: b.config.PollingConfig,
433-
IamInstanceProfile: b.config.IamInstanceProfile,
434-
SkipProfileValidation: b.config.SkipProfileValidation,
435-
SSMAgentEnabled: b.config.SSMAgentEnabled(),
432+
PollingConfig: b.config.PollingConfig,
433+
IamInstanceProfile: b.config.IamInstanceProfile,
434+
SkipProfileValidation: b.config.SkipProfileValidation,
435+
SSMAgentEnabled: b.config.SSMAgentEnabled(),
436+
IsRestricted: b.config.IsChinaCloud(),
436437
TemporaryIamInstanceProfilePolicyDocument: b.config.TemporaryIamInstanceProfilePolicyDocument,
437438
Tags: b.config.RunTags,
438439
Ctx: b.config.ctx,

builder/ebsvolume/builder.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -315,10 +315,11 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
315315
Ctx: b.config.ctx,
316316
},
317317
&awscommon.StepIamInstanceProfile{
318-
PollingConfig: b.config.PollingConfig,
319-
IamInstanceProfile: b.config.IamInstanceProfile,
320-
SkipProfileValidation: b.config.SkipProfileValidation,
321-
SSMAgentEnabled: b.config.SSMAgentEnabled(),
318+
PollingConfig: b.config.PollingConfig,
319+
IamInstanceProfile: b.config.IamInstanceProfile,
320+
SkipProfileValidation: b.config.SkipProfileValidation,
321+
SSMAgentEnabled: b.config.SSMAgentEnabled(),
322+
IsRestricted: b.config.IsChinaCloud(),
322323
TemporaryIamInstanceProfilePolicyDocument: b.config.TemporaryIamInstanceProfilePolicyDocument,
323324
Tags: b.config.RunTags,
324325
Ctx: b.config.ctx,

builder/instance/builder.go

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

0 commit comments

Comments
 (0)