1
1
#! /usr/bin/env bash
2
2
3
- aws_cmd=${aws_cmd:- aws}
4
-
5
3
# Bucket name must be all lowercase, and start/end with lowecase letter or number
6
4
# $(echo...) code to work with versions of bash older than 4.0
7
5
@@ -26,63 +24,64 @@ IDENTITY_POOL_ID=""
26
24
USER_POOL_ID=" "
27
25
USER_POOL_CLIENT_ID=" "
28
26
27
+
29
28
createCognitoResources () {
30
29
# Create a Cognito Identity and Set roles
31
- $aws_cmd cognito-identity create-identity-pool --identity-pool-name $IDENTITY_POOL_NAME --allow-unauthenticated-identities --region $REGION | grep IdentityPoolId | awk ' {print $2}' | xargs | sed -e ' s/^"//' -e ' s/"$//' -e ' s/,$//' > /tmp/poolId
30
+ aws cognito-identity create-identity-pool --identity-pool-name $IDENTITY_POOL_NAME --allow-unauthenticated-identities --region $REGION | grep IdentityPoolId | awk ' {print $2}' | xargs | sed -e ' s/^"//' -e ' s/"$//' -e ' s/,$//' > /tmp/poolId
32
31
IDENTITY_POOL_ID=$( cat /tmp/poolId)
33
32
echo " Created an identity pool with id of " $IDENTITY_POOL_ID
34
33
35
34
# Create an IAM role for unauthenticated users
36
35
cat unauthrole-trust-policy.json | sed ' s/IDENTITY_POOL/' $IDENTITY_POOL_ID ' /' > /tmp/unauthrole-trust-policy.json
37
- $aws_cmd iam create-role --role-name $ROLE_NAME_PREFIX -unauthenticated-role --assume-role-policy-document file:///tmp/unauthrole-trust-policy.json > /tmp/iamUnauthRole
36
+ aws iam create-role --role-name $ROLE_NAME_PREFIX -unauthenticated-role --assume-role-policy-document file:///tmp/unauthrole-trust-policy.json > /tmp/iamUnauthRole
38
37
if [ $? -eq 0 ]
39
38
then
40
39
echo " IAM unauthenticated role successfully created"
41
40
else
42
41
echo " Using the existing role ..."
43
- $aws_cmd iam get-role --role-name $ROLE_NAME_PREFIX -unauthenticated-role > /tmp/iamUnauthRole
44
- $aws_cmd iam update-assume-role-policy --role-name $ROLE_NAME_PREFIX -unauthenticated-role --policy-document file:///tmp/unauthrole-trust-policy.json
42
+ aws iam get-role --role-name $ROLE_NAME_PREFIX -unauthenticated-role > /tmp/iamUnauthRole
43
+ aws iam update-assume-role-policy --role-name $ROLE_NAME_PREFIX -unauthenticated-role --policy-document file:///tmp/unauthrole-trust-policy.json
45
44
fi
46
- $aws_cmd iam put-role-policy --role-name $ROLE_NAME_PREFIX -unauthenticated-role --policy-name CognitoPolicy --policy-document file://unauthrole.json
45
+ aws iam put-role-policy --role-name $ROLE_NAME_PREFIX -unauthenticated-role --policy-name CognitoPolicy --policy-document file://unauthrole.json
47
46
48
47
# Create an IAM role for authenticated users
49
48
cat authrole-trust-policy.json | sed ' s/IDENTITY_POOL/' $IDENTITY_POOL_ID ' /' > /tmp/authrole-trust-policy.json
50
- $aws_cmd iam create-role --role-name $ROLE_NAME_PREFIX -authenticated-role --assume-role-policy-document file:///tmp/authrole-trust-policy.json > /tmp/iamAuthRole
49
+ aws iam create-role --role-name $ROLE_NAME_PREFIX -authenticated-role --assume-role-policy-document file:///tmp/authrole-trust-policy.json > /tmp/iamAuthRole
51
50
if [ $? -eq 0 ]
52
51
then
53
52
echo " IAM authenticated role successfully created"
54
53
else
55
54
echo " Using the existing role ..."
56
- $aws_cmd iam get-role --role-name $ROLE_NAME_PREFIX -authenticated-role > /tmp/iamAuthRole
57
- $aws_cmd iam update-assume-role-policy --role-name $ROLE_NAME_PREFIX -authenticated-role --policy-document file:///tmp/authrole-trust-policy.json
55
+ aws iam get-role --role-name $ROLE_NAME_PREFIX -authenticated-role > /tmp/iamAuthRole
56
+ aws iam update-assume-role-policy --role-name $ROLE_NAME_PREFIX -authenticated-role --policy-document file:///tmp/authrole-trust-policy.json
58
57
fi
59
58
cat authrole.json | sed ' s~DDB_TABLE_ARN~' $DDB_TABLE_ARN ' ~' > /tmp/authrole.json
60
- $aws_cmd iam put-role-policy --role-name $ROLE_NAME_PREFIX -authenticated-role --policy-name CognitoPolicy --policy-document file:///tmp/authrole.json
59
+ aws iam put-role-policy --role-name $ROLE_NAME_PREFIX -authenticated-role --policy-name CognitoPolicy --policy-document file:///tmp/authrole.json
61
60
62
61
# Create the user pool
63
- $aws_cmd cognito-idp create-user-pool --pool-name $POOL_NAME --auto-verified-attributes email --policies file://user-pool-policy.json --region $REGION > /tmp/$POOL_NAME -create-user-pool
62
+ aws cognito-idp create-user-pool --pool-name $POOL_NAME --auto-verified-attributes email --policies file://user-pool-policy.json --region $REGION > /tmp/$POOL_NAME -create-user-pool
64
63
USER_POOL_ID=$( grep -E ' "Id":' /tmp/$POOL_NAME -create-user-pool | awk -F' "' ' {print $4}' )
65
64
echo " Created user pool with an id of " $USER_POOL_ID
66
65
67
66
# Create the user pool client
68
- $aws_cmd cognito-idp create-user-pool-client --user-pool-id $USER_POOL_ID --no-generate-secret --client-name webapp --region $REGION > /tmp/$POOL_NAME -create-user-pool-client
67
+ aws cognito-idp create-user-pool-client --user-pool-id $USER_POOL_ID --no-generate-secret --client-name webapp --region $REGION > /tmp/$POOL_NAME -create-user-pool-client
69
68
USER_POOL_CLIENT_ID=$( grep -E ' "ClientId":' /tmp/$POOL_NAME -create-user-pool-client | awk -F' "' ' {print $4}' )
70
69
echo " Created user pool client with id of " $USER_POOL_CLIENT_ID
71
70
72
71
# Add the user pool and user pool client id to the identity pool
73
- $aws_cmd cognito-identity update-identity-pool --allow-unauthenticated-identities --identity-pool-id $IDENTITY_POOL_ID --identity-pool-name $IDENTITY_POOL_NAME \
72
+ aws cognito-identity update-identity-pool --allow-unauthenticated-identities --identity-pool-id $IDENTITY_POOL_ID --identity-pool-name $IDENTITY_POOL_NAME \
74
73
--cognito-identity-providers ProviderName=cognito-idp.$REGION .amazonaws.com/$USER_POOL_ID ,ClientId=$USER_POOL_CLIENT_ID --region $REGION \
75
74
> /tmp/$IDENTITY_POOL_ID -add-user-pool
76
75
77
76
# Update cognito identity with the roles
78
77
UNAUTH_ROLE_ARN=$( perl -nle ' print $& if m{"Arn":\s*"\K([^"]*)}' /tmp/iamUnauthRole | awk -F' "' ' {print $1}' )
79
78
AUTH_ROLE_ARN=$( perl -nle ' print $& if m{"Arn":\s*"\K([^"]*)}' /tmp/iamAuthRole | awk -F' "' ' {print $1}' )
80
- $aws_cmd cognito-identity set-identity-pool-roles --identity-pool-id $IDENTITY_POOL_ID --roles authenticated=$AUTH_ROLE_ARN ,unauthenticated=$UNAUTH_ROLE_ARN --region $REGION
79
+ aws cognito-identity set-identity-pool-roles --identity-pool-id $IDENTITY_POOL_ID --roles authenticated=$AUTH_ROLE_ARN ,unauthenticated=$UNAUTH_ROLE_ARN --region $REGION
81
80
}
82
81
83
82
createDDBTable () {
84
83
# Create DDB Table
85
- $aws_cmd dynamodb create-table \
84
+ aws dynamodb create-table \
86
85
--table-name $TABLE_NAME \
87
86
--attribute-definitions \
88
87
AttributeName=userId,AttributeType=S \
@@ -97,7 +96,7 @@ createDDBTable() {
97
96
echo " DynamoDB table successfully created"
98
97
else
99
98
echo " Using the existing table ..."
100
- $aws_cmd dynamodb describe-table --table-name $TABLE_NAME > /tmp/dynamoTable
99
+ aws dynamodb describe-table --table-name $TABLE_NAME > /tmp/dynamoTable
101
100
fi
102
101
103
102
DDB_TABLE_ARN=$( perl -nle ' print $& if m{"TableArn":\s*"\K([^"]*)}' /tmp/dynamoTable | awk -F' "' ' {print $1}' )
129
128
130
129
createS3Bucket () {
131
130
# Create the bucket
132
- $aws_cmd s3 mb s3://$BUCKET_NAME / --region $REGION 2> /tmp/s3-mb-status
131
+ aws s3 mb s3://$BUCKET_NAME / --region $REGION 2> /tmp/s3-mb-status
133
132
status=$?
134
133
135
134
if [ $status -eq 0 ]
@@ -153,19 +152,19 @@ createS3Bucket() {
153
152
154
153
uploadS3Bucket () {
155
154
# Add the ‘website’ configuration and bucket policy
156
- $aws_cmd s3 website s3://$BUCKET_NAME / --index-document index.html --error-document index.html --region $REGION
155
+ aws s3 website s3://$BUCKET_NAME / --index-document index.html --error-document index.html --region $REGION
157
156
cat s3-bucket-policy.json | sed ' s/BUCKET_NAME/' $BUCKET_NAME ' /' > /tmp/s3-bucket-policy.json
158
- $aws_cmd s3api put-bucket-policy --bucket $BUCKET_NAME --policy file:///tmp/s3-bucket-policy.json --region $REGION
157
+ aws s3api put-bucket-policy --bucket $BUCKET_NAME --policy file:///tmp/s3-bucket-policy.json --region $REGION
159
158
# Build the project and sync it up to the bucket
160
159
if [ ! -d " $NPM_DIR " ]; then
161
160
npm install
162
161
fi
163
162
cd ..
164
163
echo " Building the project"
165
- ng build $( if [ " $aws_cmd " == " awslocal " ] ; then echo " --base-href / $BUCKET_NAME / " ; fi )
164
+ ng build
166
165
cd -
167
166
echo " Syncing files to the S3 bucket from " $ROOT_DIR /dist/
168
- $aws_cmd s3 sync $ROOT_DIR /dist/ s3://$BUCKET_NAME / --region $REGION
167
+ aws s3 sync $ROOT_DIR /dist/ s3://$BUCKET_NAME / --region $REGION
169
168
}
170
169
171
170
printConfig () {
@@ -208,13 +207,7 @@ export const environment = {
208
207
albumName: "usercontent",
209
208
bucketRegion: '$REGION ',
210
209
211
- ddbTableName: '$TABLE_NAME ',
212
-
213
- cognito_idp_endpoint: '$( if [ " $aws_cmd " == " awslocal" ]; then echo ' http://localhost:4590' ; fi ) ',
214
- cognito_identity_endpoint: '$( if [ " $aws_cmd " == " awslocal" ]; then echo ' http://localhost:4591' ; fi ) ',
215
- sts_endpoint: '$( if [ " $aws_cmd " == " awslocal" ]; then echo ' http://localhost:4592' ; fi ) ',
216
- dynamodb_endpoint: '$( if [ " $aws_cmd " == " awslocal" ]; then echo ' http://localhost:4569' ; fi ) ',
217
- s3_endpoint: '$( if [ " $aws_cmd " == " awslocal" ]; then echo ' http://localhost:4572' ; fi ) '
210
+ ddbTableName: '$TABLE_NAME '
218
211
};
219
212
220
213
EOF
@@ -235,13 +228,7 @@ export const environment = {
235
228
albumName: "usercontent",
236
229
bucketRegion: '$REGION ',
237
230
238
- ddbTableName: '$TABLE_NAME ',
239
-
240
- cognito_idp_endpoint: '$( if [ " $aws_cmd " == " awslocal" ]; then echo ' http://localhost:4590' ; fi ) ',
241
- cognito_identity_endpoint: '$( if [ " $aws_cmd " == " awslocal" ]; then echo ' http://localhost:4591' ; fi ) ',
242
- sts_endpoint: '$( if [ " $aws_cmd " == " awslocal" ]; then echo ' http://localhost:4592' ; fi ) ',
243
- dynamodb_endpoint: '$( if [ " $aws_cmd " == " awslocal" ]; then echo ' http://localhost:4569' ; fi ) ',
244
- s3_endpoint: '$( if [ " $aws_cmd " == " awslocal" ]; then echo ' http://localhost:4572' ; fi ) '
231
+ ddbTableName: '$TABLE_NAME '
245
232
};
246
233
247
234
EOF
0 commit comments