Skip to content

Commit 9f249f9

Browse files
committed
convert line delimiter
fix bug creation instance profile
1 parent 8fefa83 commit 9f249f9

File tree

4 files changed

+58
-46
lines changed

4 files changed

+58
-46
lines changed

template/awscli/create_ec2_profile_cross_account

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,17 @@ if [ -z "$CROSS_ACCOUNT_PROFILE" ]; then
4848
exit 1
4949
fi
5050

51+
if [ -z "$S3_BUCKET" ]; then
52+
echo 'S3 bucket name is required.'
53+
exit 1
54+
fi
55+
56+
if [ -z "$KEY_ALIAS" ]; then
57+
echo 'Key alias for CodePipeline is required.'
58+
exit 1
59+
fi
60+
61+
5162
KMS_KEY_POLICY_NAME="KMSKeyPolicy-$(aws sts get-caller-identity | jq -r '.Account')-$KEY_ALIAS"
5263
S3_BUCKET_POLICY_NAME="S3BucketPolicy-$S3_BUCKET"
5364

@@ -66,6 +77,7 @@ if [ -z $KMS_KEY_POLICY_ARN ]; then
6677
KMS_KEY_ALIAS="alias/$KEY_ALIAS"
6778
KMS_KEY_ID="$(aws kms list-aliases 2> /dev/null | jq -c --arg alias $KMS_KEY_ALIAS '.Aliases[] | select(.AliasName == $alias)' | jq -r '.TargetKeyId')"
6879
KMS_KEY_ARN="$(aws kms describe-key --key-id $KMS_KEY_ID 2> /dev/null | jq -r '.KeyMetadata.Arn')"
80+
REPLACE_STR='\/'
6981
KMS_KEY_ARN="${KMS_KEY_ARN//\//$REPLACE_STR}"
7082

7183
# Generate temporory policy file
@@ -75,7 +87,7 @@ if [ -z $KMS_KEY_POLICY_ARN ]; then
7587
> .config/$KMS_KEY_POLICY_FILE_NAME
7688

7789
# create codepipeline service policy
78-
KMS_KEY_POLICY_ARN="$(aws iam create-policy --profile $CROSS_ACCOUNT_PROFILE --policy-name $KMS_KEY_POLICY_ARN --path /service-role/ --policy-document file://.config/$KMS_KEY_POLICY_FILE_NAME | jq .Policy.Arn | tr -d \")"
90+
KMS_KEY_POLICY_ARN="$(aws iam create-policy --profile $CROSS_ACCOUNT_PROFILE --policy-name $KMS_KEY_POLICY_NAME --path /service-role/ --policy-document file://.config/$KMS_KEY_POLICY_FILE_NAME | jq .Policy.Arn | tr -d \")"
7991
fi
8092

8193
# S3 bucket policy
@@ -95,26 +107,26 @@ ROLE_ARN="$(aws iam get-role --profile $CROSS_ACCOUNT_PROFILE --role-name $INSTA
95107
if [ -z $ROLE_ARN ]; then
96108
# create ec2 role for codedeploy
97109
aws iam create-role \
98-
--profile $CROSS_ACCOUNT_PROFILE
110+
--profile $CROSS_ACCOUNT_PROFILE \
99111
--role-name $INSTACNE_PROFILE_NAME \
100112
--path /service-role/ \
101113
--assume-role-policy-document file://policies/TrustPolicyforEC2.json
102114

103115
# attach policies
104116
aws iam attach-role-policy \
105-
--profile $CROSS_ACCOUNT_PROFILE
117+
--profile $CROSS_ACCOUNT_PROFILE \
106118
--policy-arn $KMS_KEY_POLICY_ARN \
107119
--role-name $INSTACNE_PROFILE_NAME
108120

109121
aws iam attach-role-policy \
110-
--profile $CROSS_ACCOUNT_PROFILE
122+
--profile $CROSS_ACCOUNT_PROFILE \
111123
--policy-arn $S3_BUCKET_POLICY_ARN \
112124
--role-name $INSTACNE_PROFILE_NAME
113125
fi
114126

115127
# Create the instance profile required by EC2 to contain the role
116128
aws iam create-instance-profile \
117-
--profile $CROSS_ACCOUNT_PROFILE
129+
--profile $CROSS_ACCOUNT_PROFILE \
118130
--instance-profile-name $INSTACNE_PROFILE_NAME
119131

120132
# Finally, add the role to the instance profile
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
{
2-
"Version": "2012-10-17",
3-
"Statement": {
4-
"Effect": "Allow",
5-
"Action": "sts:AssumeRole",
6-
"Resource": [
7-
"arn:aws:iam::ACCOUNT-B:role/*"
8-
]
9-
}
1+
{
2+
"Version": "2012-10-17",
3+
"Statement": {
4+
"Effect": "Allow",
5+
"Action": "sts:AssumeRole",
6+
"Resource": [
7+
"arn:aws:iam::ACCOUNT-B:role/*"
8+
]
9+
}
1010
}
Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
{
2-
"Version": "2012-10-17",
3-
"Statement": [
4-
{
5-
"Effect": "Allow",
6-
"Action": [
7-
"kms:DescribeKey",
8-
"kms:GenerateDataKey*",
9-
"kms:Encrypt",
10-
"kms:ReEncrypt*",
11-
"kms:Decrypt"
12-
],
13-
"Resource": [
14-
"KMS-KEY-ARN"
15-
]
16-
}
17-
]
1+
{
2+
"Version": "2012-10-17",
3+
"Statement": [
4+
{
5+
"Effect": "Allow",
6+
"Action": [
7+
"kms:DescribeKey",
8+
"kms:GenerateDataKey*",
9+
"kms:Encrypt",
10+
"kms:ReEncrypt*",
11+
"kms:Decrypt"
12+
],
13+
"Resource": [
14+
"KMS-KEY-ARN"
15+
]
16+
}
17+
]
1818
}
Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
{
2-
"Version": "2012-10-17",
3-
"Statement": [
4-
{
5-
"Effect": "Allow",
6-
"Action": [
7-
"s3:Get*",
8-
"s3:ListBucket"
9-
],
10-
"Resource": [
11-
"arn:aws:s3:::S3-BUCKET",
12-
"arn:aws:s3:::S3-BUCKET/*"
13-
]
14-
}
15-
]
1+
{
2+
"Version": "2012-10-17",
3+
"Statement": [
4+
{
5+
"Effect": "Allow",
6+
"Action": [
7+
"s3:Get*",
8+
"s3:ListBucket"
9+
],
10+
"Resource": [
11+
"arn:aws:s3:::S3-BUCKET",
12+
"arn:aws:s3:::S3-BUCKET/*"
13+
]
14+
}
15+
]
1616
}

0 commit comments

Comments
 (0)