Skip to content
This repository was archived by the owner on Jun 26, 2020. It is now read-only.

Commit 0093626

Browse files
author
Jasmine Wang
committed
Added Elastic Beanstalk support to Cognito quickstart app
1 parent 51070ff commit 0093626

16 files changed

+471
-90
lines changed

Diff for: .ebextensions/app.config

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
packages:
2+
yum:
3+
krb5-devel: []

Diff for: .ebextensions/build-angular.config

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
files:
2+
"/opt/elasticbeanstalk/hooks/appdeploy/pre/55_build_angular.sh":
3+
mode: "000755"
4+
owner: root
5+
group: root
6+
content: |
7+
#!/usr/bin/env bash
8+
9+
set -xe
10+
11+
EB_APP_CURRENT_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k app_deploy_dir)
12+
EB_APP_STAGING_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k app_staging_dir)
13+
EB_APP_USER=$(/opt/elasticbeanstalk/bin/get-config container -k app_user)
14+
EB_APP_STAGING_NM_DIR=$EB_APP_STAGING_DIR/node_modules/.bin
15+
16+
NODE_PATH=$(/opt/elasticbeanstalk/bin/get-config container -k nodejs_install_dir)
17+
NODE_PATH_FULL=`ls -td $NODE_PATH/node-* | head -1`/bin
18+
export PATH=$NODE_PATH_FULL:$PATH
19+
export PATH=$EB_APP_STAGING_NM_DIR:$PATH
20+
21+
cd $EB_APP_STAGING_DIR
22+
npm run build

Diff for: .ebextensions/nodecommand.config

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
option_settings:
2+
aws:elasticbeanstalk:container:nodejs:
3+
NodeCommand: "npm start"

Diff for: .gitignore

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# See http://help.github.com/ignore-files/ for more about ignoring files.
22

33
# compiled output
4-
/dist
4+
/dist/**/*
55
/tmp
66

77
# dependencies
8-
/node_modules
8+
/node_modules/**/*
99
/src/bower_components
1010
/public/bower_components
1111

@@ -28,3 +28,13 @@ testem.log
2828
#System Files
2929
.DS_Store
3030
Thumbs.db
31+
32+
# Elastic Beanstalk Files
33+
.elasticbeanstalk/*
34+
!.elasticbeanstalk/*.cfg.yml
35+
!.elasticbeanstalk/*.global.yml
36+
37+
# Build Artifacts
38+
Archive.zip
39+
test.pem
40+
resetConfig.sh

Diff for: app.js

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
var express = require('express');
2+
var path = require('path');
3+
var favicon = require('serve-favicon');
4+
var logger = require('morgan');
5+
var cookieParser = require('cookie-parser');
6+
var bodyParser = require('body-parser');
7+
8+
var routes = require('./routes/index');
9+
var api = require('./routes/api');
10+
11+
var app = express();
12+
13+
// view engine setup
14+
app.set('views', path.join(__dirname, 'dist'));
15+
app.set('view engine', 'ejs');
16+
app.engine('html', require('ejs').renderFile);
17+
18+
// uncomment after placing your favicon in /public
19+
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
20+
app.use(logger('dev'));
21+
app.use(bodyParser.json());
22+
app.use(bodyParser.urlencoded({ extended: false }));
23+
app.use(cookieParser());
24+
app.use(express.static(path.join(__dirname, 'dist')));
25+
26+
app.use('/api', api);
27+
app.use('/', routes);
28+
29+
// catch 404 and forward to error handler
30+
app.use(function(req, res, next) {
31+
var err = new Error('Not Found');
32+
err.status = 404;
33+
next(err);
34+
});
35+
36+
// error handlers
37+
38+
// development error handler
39+
// will print stacktrace
40+
if (app.get('env') === 'development') {
41+
app.use(function(err, req, res, next) {
42+
res.status(err.status || 500);
43+
res.render('error', {
44+
message: err.message,
45+
error: err
46+
});
47+
});
48+
}
49+
50+
// production error handler
51+
// no stacktraces leaked to user
52+
app.use(function(err, req, res, next) {
53+
res.status(err.status || 500);
54+
res.render('error', {
55+
message: err.message,
56+
error: {}
57+
});
58+
});
59+
60+
61+
module.exports = app;

Diff for: app_config.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"AWS_REGION": "us-west-2",
3+
"AWS_COGNITO_USERPOOLID": "us-west-2_yv5XO7TOC",
4+
"AWS_COGNITO_CLIENTID": "7euuf658388svpl2rnfdcqn626",
5+
"AWS_COGNITO_IDENTITY_POOL_ID": "us-west-2:b4e35c1b-6b80-4dda-8111-f5db206d4a11",
6+
"AWS_DYNAMODB_TABLENAME": "LoginTrailbudilovdeletecogdemo"
7+
}

Diff for: aws/authrole.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"dynamodb:DeleteItem"
2424
],
2525
"Resource": [
26-
"arn:aws:dynamodb:REGION:ACCOUNT_NUMBER:table/TABLE_NAME"
26+
"DDB_TABLE_ARN"
2727
],
2828
"Condition": {
2929
"ForAllValues:StringEquals": {

Diff for: aws/createResources.sh

100644100755
+175-57
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,193 @@
11
#!/usr/bin/env bash
22

3-
ROOT_NAME=budilovdelete
3+
ROOT_NAME=budilovdeletecogdemo
44
# Bucket name must be all lowercase, and start/end with lowecase letter or number
55
# $(echo...) code to work with versions of bash older than 4.0
66
BUCKET_NAME=budilov-$(echo "$ROOT_NAME" | tr '[:upper:]' '[:lower:]')
77
TABLE_NAME=LoginTrail$ROOT_NAME
88

9-
# Replace with your 12-digit AWS account ID (e.g., 123456789012)
10-
AWS_ACCOUNT=540403165297
119
ROLE_NAME_PREFIX=$ROOT_NAME
1210
POOL_NAME=$ROOT_NAME
1311
IDENTITY_POOL_NAME=$ROOT_NAME
1412
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
15181

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
69182

70183
sleep 3
71184
echo "Region: " $REGION
72185
echo "DynamoDB: " $TABLE_NAME
73186
echo "Bucket name: " + $BUCKET_NAME
74187
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

Comments
 (0)