|
1 | 1 | #!/usr/bin/env bash
|
2 | 2 |
|
3 |
| -ROOT_NAME=budilovdelete |
| 3 | +ROOT_NAME=budilovdeletecogdemo |
4 | 4 | # Bucket name must be all lowercase, and start/end with lowecase letter or number
|
5 | 5 | # $(echo...) code to work with versions of bash older than 4.0
|
6 | 6 | BUCKET_NAME=budilov-$(echo "$ROOT_NAME" | tr '[:upper:]' '[:lower:]')
|
7 | 7 | TABLE_NAME=LoginTrail$ROOT_NAME
|
8 | 8 |
|
9 |
| -# Replace with your 12-digit AWS account ID (e.g., 123456789012) |
10 |
| -AWS_ACCOUNT=540403165297 |
11 | 9 | ROLE_NAME_PREFIX=$ROOT_NAME
|
12 | 10 | POOL_NAME=$ROOT_NAME
|
13 | 11 | IDENTITY_POOL_NAME=$ROOT_NAME
|
14 | 12 | REGION=us-west-2
|
| 13 | +EB_INSTANCE_TYPE=t2.small |
| 14 | +EB_PLATFORM=node.js |
| 15 | +CURR_DIR=$( cd $(dirname $0) ; pwd -P ) |
| 16 | + |
| 17 | +DDB_TABLE_ARN="" |
| 18 | +IDENTITY_POOL_ID="" |
| 19 | +USER_POOL_ID="" |
| 20 | +USER_POOL_CLIENT_ID="" |
| 21 | + |
| 22 | + |
| 23 | +createS3Bucket() { |
| 24 | + # Create the bucket |
| 25 | + aws s3 mb s3://$BUCKET_NAME/ --region $REGION |
| 26 | + # Add the ‘website’ configuration and bucket policy |
| 27 | + aws s3 website s3://$BUCKET_NAME/ --index-document index.html --error-document index.html |
| 28 | + cat s3-bucket-policy.json | sed 's/BUCKET_NAME/'$BUCKET_NAME'/' > /tmp/s3-bucket-policy.json |
| 29 | + aws s3api put-bucket-policy --bucket $BUCKET_NAME --policy file:///tmp/s3-bucket-policy.json |
| 30 | + #Build the project and sync it up to the bucket |
| 31 | + ng build --prod ../ |
| 32 | + aws s3 sync ../dist/ s3://$BUCKET_NAME/ |
| 33 | +} |
| 34 | + |
| 35 | +createDDBTable() { |
| 36 | + # Create DDB Table |
| 37 | + aws dynamodb create-table \ |
| 38 | + --table-name $TABLE_NAME \ |
| 39 | + --attribute-definitions \ |
| 40 | + AttributeName=userId,AttributeType=S \ |
| 41 | + AttributeName=activityDate,AttributeType=S \ |
| 42 | + --key-schema AttributeName=userId,KeyType=HASH AttributeName=activityDate,KeyType=RANGE \ |
| 43 | + --provisioned-throughput ReadCapacityUnits=1,WriteCapacityUnits=1 \ |
| 44 | + --region $REGION \ |
| 45 | + > /tmp/dynamoTable |
| 46 | + |
| 47 | + DDB_TABLE_ARN=$(perl -nle 'print $& if m{"TableArn":\s*"\K([^"]*)}' /tmp/dynamoTable | awk -F'"' '{print $1}') |
| 48 | +} |
| 49 | + |
| 50 | +createCognitoResources() { |
| 51 | + # Create a Cognito Identity and Set roles |
| 52 | + 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 |
| 53 | + IDENTITY_POOL_ID=$(cat /tmp/poolId) |
| 54 | + echo "Created an identity pool with id of " $IDENTITY_POOL_ID |
| 55 | + |
| 56 | + # Create an IAM role for unauthenticated users |
| 57 | + cat unauthrole-trust-policy.json | sed 's/IDENTITY_POOL/'$IDENTITY_POOL_ID'/' > /tmp/unauthrole-trust-policy.json |
| 58 | + aws iam create-role --role-name $ROLE_NAME_PREFIX-unauthenticated-role --assume-role-policy-document file:///tmp/unauthrole-trust-policy.json > /tmp/iamUnauthRole |
| 59 | + aws iam put-role-policy --role-name $ROLE_NAME_PREFIX-unauthenticated-role --policy-name CognitoPolicy --policy-document file://unauthrole.json |
| 60 | + |
| 61 | + # Create an IAM role for authenticated users |
| 62 | + cat authrole-trust-policy.json | sed 's/IDENTITY_POOL/'$IDENTITY_POOL_ID'/' > /tmp/authrole-trust-policy.json |
| 63 | + aws iam create-role --role-name $ROLE_NAME_PREFIX-authenticated-role --assume-role-policy-document file:///tmp/authrole-trust-policy.json > /tmp/iamAuthRole |
| 64 | + cat authrole.json | sed 's~DDB_TABLE_ARN~'$DDB_TABLE_ARN'~' > /tmp/authrole.json |
| 65 | + aws iam put-role-policy --role-name $ROLE_NAME_PREFIX-authenticated-role --policy-name CognitoPolicy --policy-document file:///tmp/authrole.json |
| 66 | + |
| 67 | + # Create the user pool |
| 68 | + 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 |
| 69 | + USER_POOL_ID=$(grep -E '"Id":' /tmp/$POOL_NAME-create-user-pool | awk -F'"' '{print $4}') |
| 70 | + echo "Created user pool with an id of " $USER_POOL_ID |
| 71 | + |
| 72 | + # Create the user pool client |
| 73 | + 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 |
| 74 | + USER_POOL_CLIENT_ID=$(grep -E '"ClientId":' /tmp/$POOL_NAME-create-user-pool-client | awk -F'"' '{print $4}') |
| 75 | + echo "Created user pool client with id of " $USER_POOL_CLIENT_ID |
| 76 | + |
| 77 | + # Add the user pool and user pool client id to the identity pool |
| 78 | + aws cognito-identity update-identity-pool --allow-unauthenticated-identities --identity-pool-id $IDENTITY_POOL_ID --identity-pool-name $IDENTITY_POOL_NAME --cognito-identity-providers ProviderName=cognito-idp.$REGION.amazonaws.com/$USER_POOL_ID,ClientId=$USER_POOL_CLIENT_ID --region $REGION |
| 79 | + |
| 80 | + # Update cognito identity with the roles |
| 81 | + UNAUTH_ROLE_ARN=$(perl -nle 'print $& if m{"Arn":\s*"\K([^"]*)}' /tmp/iamUnauthRole | awk -F'"' '{print $1}') |
| 82 | + AUTH_ROLE_ARN=$(perl -nle 'print $& if m{"Arn":\s*"\K([^"]*)}' /tmp/iamAuthRole | awk -F'"' '{print $1}') |
| 83 | + aws cognito-identity set-identity-pool-roles --identity-pool-id $IDENTITY_POOL_ID --roles authenticated=$AUTH_ROLE_ARN,unauthenticated=$UNAUTH_ROLE_ARN --region $REGION |
| 84 | +} |
| 85 | + |
| 86 | +createEBResources() { |
| 87 | + cd $CURR_DIR/../ |
| 88 | + sleep 1 |
| 89 | + eb init $ROOT_NAME --region $REGION --platform $EB_PLATFORM |
| 90 | + sleep 1 |
| 91 | + eb create $ROOT_NAME -d --region $REGION --platform $EB_PLATFORM --instance_type $EB_INSTANCE_TYPE |
| 92 | + cd $CURR_DIR |
| 93 | +} |
| 94 | + |
| 95 | +verifyEBCLI() { |
| 96 | + if command -v eb >/dev/null; then |
| 97 | + echo "Creating Elastic Beanstalk environment ..." |
| 98 | + createEBResources |
| 99 | + else |
| 100 | + echo "Please install the Elastic Beanstalk Command Line Interface first" |
| 101 | + exit 1; |
| 102 | + fi |
| 103 | +} |
| 104 | + |
| 105 | +writeConfigFiles() { |
| 106 | +( |
| 107 | +cat <<EOF |
| 108 | +export const environment = { |
| 109 | + production: false, |
| 110 | +
|
| 111 | + region: '$REGION', |
| 112 | +
|
| 113 | + identityPoolId: '$IDENTITY_POOL_ID', |
| 114 | + userPoolId: '$USER_POOL_ID', |
| 115 | + clientId: '$USER_POOL_CLIENT_ID', |
| 116 | +
|
| 117 | + rekognitionBucket: 'rekognition-pics', |
| 118 | + albumName: "usercontent", |
| 119 | + bucketRegion: '$REGION', |
| 120 | +
|
| 121 | + ddbTableName: '$TABLE_NAME' |
| 122 | +}; |
| 123 | +
|
| 124 | +EOF |
| 125 | +) > $CURR_DIR/../src/environments/environment.ts |
| 126 | + |
| 127 | +( |
| 128 | +cat <<EOF |
| 129 | +export const environment = { |
| 130 | + production: true, |
| 131 | +
|
| 132 | + region: '$REGION', |
| 133 | +
|
| 134 | + identityPoolId: '$IDENTITY_POOL_ID', |
| 135 | + userPoolId: '$USER_POOL_ID', |
| 136 | + clientId: '$USER_POOL_CLIENT_ID', |
| 137 | +
|
| 138 | + rekognitionBucket: 'rekognition-pics', |
| 139 | + albumName: "usercontent", |
| 140 | + bucketRegion: '$REGION', |
| 141 | +
|
| 142 | + ddbTableName: '$TABLE_NAME' |
| 143 | +}; |
| 144 | +
|
| 145 | +EOF |
| 146 | +) > $CURR_DIR/../src/environments/environment.prod.ts |
| 147 | + |
| 148 | +cd $CURR_DIR/../ |
| 149 | +git add . |
| 150 | +git commit -m "Updated config files for created resources" |
| 151 | +cd $CURR_DIR |
| 152 | + |
| 153 | +} |
| 154 | + |
| 155 | + |
| 156 | +PS3='Where would you like to deploy your application? ' |
| 157 | +options=("Elastic Beanstalk" "S3" "Quit") |
| 158 | +select opt in "${options[@]}" |
| 159 | +do |
| 160 | + |
| 161 | + createDDBTable |
| 162 | + createCognitoResources |
| 163 | + writeConfigFiles |
| 164 | + |
| 165 | + case $opt in |
| 166 | + "Elastic Beanstalk") |
| 167 | + verifyEBCLI |
| 168 | + break |
| 169 | + ;; |
| 170 | + "S3") |
| 171 | +# echo "you chose S3" |
| 172 | + createS3Bucket |
| 173 | + break |
| 174 | + ;; |
| 175 | + "Quit") |
| 176 | + break |
| 177 | + ;; |
| 178 | + *) echo invalid option;; |
| 179 | + esac |
| 180 | +done |
15 | 181 |
|
16 |
| -# Create the bucket |
17 |
| -aws s3 mb s3://$BUCKET_NAME/ --region $REGION |
18 |
| -# Add the ‘website’ configuration and bucket policy |
19 |
| -aws s3 website s3://$BUCKET_NAME/ --index-document index.html --error-document index.html |
20 |
| -cat s3-bucket-policy.json | sed 's/BUCKET_NAME/'$BUCKET_NAME'/' > /tmp/s3-bucket-policy.json |
21 |
| -aws s3api put-bucket-policy --bucket $BUCKET_NAME --policy file:///tmp/s3-bucket-policy.json |
22 |
| -#Build the project and sync it up to the bucket |
23 |
| -ng build --prod ../ |
24 |
| -aws s3 sync ../dist/ s3://$BUCKET_NAME/ |
25 |
| - |
26 |
| -# Create DDB Table |
27 |
| -aws dynamodb create-table \ |
28 |
| - --table-name $TABLE_NAME \ |
29 |
| - --attribute-definitions \ |
30 |
| - AttributeName=userId,AttributeType=S \ |
31 |
| - AttributeName=activityDate,AttributeType=S \ |
32 |
| - --key-schema AttributeName=userId,KeyType=HASH AttributeName=activityDate,KeyType=RANGE \ |
33 |
| - --provisioned-throughput ReadCapacityUnits=1,WriteCapacityUnits=1 \ |
34 |
| - --region $REGION |
35 |
| - |
36 |
| - |
37 |
| -# Create a Cognito Identity and Set roles |
38 |
| -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 |
39 |
| -identityPoolId=$(cat /tmp/poolId) |
40 |
| -echo "Created an identity pool with id of " $identityPoolId |
41 |
| - |
42 |
| -# Create an IAM role for unauthenticated users |
43 |
| -cat unauthrole-trust-policy.json | sed 's/IDENTITY_POOL/'$identityPoolId'/' > /tmp/unauthrole-trust-policy.json |
44 |
| -aws iam create-role --role-name $ROLE_NAME_PREFIX-unauthenticated-role --assume-role-policy-document file:///tmp/unauthrole-trust-policy.json |
45 |
| -aws iam put-role-policy --role-name $ROLE_NAME_PREFIX-unauthenticated-role --policy-name CognitoPolicy --policy-document file://unauthrole.json |
46 |
| - |
47 |
| -# Create an IAM role for authenticated users |
48 |
| -cat authrole-trust-policy.json | sed 's/IDENTITY_POOL/'$identityPoolId'/' > /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 |
50 |
| -cat authrole.json | sed 's/TABLE_NAME/'$TABLE_NAME'/' | sed 's/ACCOUNT_NUMBER/'$AWS_ACCOUNT'/' | sed 's/REGION/'$REGION'/' > /tmp/authrole.json |
51 |
| -aws iam put-role-policy --role-name $ROLE_NAME_PREFIX-authenticated-role --policy-name CognitoPolicy --policy-document file:///tmp/authrole.json |
52 |
| - |
53 |
| -# Create the user pool |
54 |
| -aws cognito-idp create-user-pool --pool-name $POOL_NAME --auto-verified-attributes email --schema Name=email,Required=true --policies file://user-pool-policy.json --region $REGION > /tmp/$POOL_NAME-create-user-pool |
55 |
| -userPoolId=$(grep -E '"Id":' /tmp/$POOL_NAME-create-user-pool | awk -F'"' '{print $4}') |
56 |
| -echo "Created user pool with an id of " $userPoolId |
57 |
| - |
58 |
| -# Create the user pool client |
59 |
| -aws cognito-idp create-user-pool-client --user-pool-id $userPoolId --no-generate-secret --client-name webapp --region $REGION > /tmp/$POOL_NAME-create-user-pool-client |
60 |
| -userPoolClientId=$(grep -E '"ClientId":' /tmp/$POOL_NAME-create-user-pool-client | awk -F'"' '{print $4}') |
61 |
| -echo "Created user pool client with id of " $userPoolClientId |
62 |
| - |
63 |
| -# Add the user pool and user pool client id to the identity pool |
64 |
| -aws cognito-identity update-identity-pool --allow-unauthenticated-identities --identity-pool-id $identityPoolId --identity-pool-name $IDENTITY_POOL_NAME --cognito-identity-providers ProviderName=cognito-idp.$REGION.amazonaws.com/$userPoolId,ClientId=$userPoolClientId --region $REGION |
65 |
| - |
66 |
| -# Update cognito identity with the roles |
67 |
| -# If this command gives you an error, associate the roles manually |
68 |
| -aws cognito-identity set-identity-pool-roles --identity-pool-id $identityPoolId --roles authenticated=arn:aws:iam::$AWS_ACCOUNT:role/$ROLE_NAME_PREFIX-authenticated-role,unauthenticated=arn:aws:iam::$AWS_ACCOUNT:role/$ROLE_NAME_PREFIX-unauthenticated-role --region $REGION |
69 | 182 |
|
70 | 183 | sleep 3
|
71 | 184 | echo "Region: " $REGION
|
72 | 185 | echo "DynamoDB: " $TABLE_NAME
|
73 | 186 | echo "Bucket name: " + $BUCKET_NAME
|
74 | 187 | echo "Identity Pool name: " $IDENTITY_POOL_NAME
|
75 |
| -echo "Identity Pool id: " $identityPoolId |
| 188 | +echo "Identity Pool id: " $IDENTITY_POOL_ID |
| 189 | + |
| 190 | + |
| 191 | + |
| 192 | + |
| 193 | + |
0 commit comments