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