-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
deploy.sh
executable file
·95 lines (76 loc) · 3.7 KB
/
deploy.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/bin/bash
AWS_CREDENTIALS_PROFILE="${AWS_CREDENTIALS_PROFILE:-default}"
read_var() {
VAR=$(grep "$1" "$2" | xargs)
IFS="=" read -ra VAR <<< "$VAR"
echo ${VAR[1]} | sed 's/"/\\"/g'
}
build() {
LAMBDA_FOLDER=$1
LAMBDA_NAME=$2
mkdir build
cp -R lambdas/$LAMBDA_FOLDER/* build
cp -R lib/* build
cp -R utils build
cd build
zip -X -r ../$LAMBDA_NAME.zip *
cd ..
rm -rf build
}
deploy() {
LAMBDA_NAME=$1
LAMBDA_TIMEOUT=$2
ENV_VARS=$3
if aws lambda get-function --profile "$AWS_CREDENTIALS_PROFILE" --function-name "$LAMBDA_NAME" 2>&1 | grep -q "Function not found"
then
aws lambda create-function --profile "$AWS_CREDENTIALS_PROFILE" --function-name "$LAMBDA_NAME" --runtime python3.6 --role "$AWS_ROLE" --handler lambda_function.lambda_handler --zip-file fileb://"$LAMBDA_NAME".zip || exit
else
aws lambda update-function-code --profile "$AWS_CREDENTIALS_PROFILE" --function-name "$LAMBDA_NAME" --zip-file fileb://"$LAMBDA_NAME".zip || exit
fi
aws lambda update-function-configuration --profile "$AWS_CREDENTIALS_PROFILE" --function-name "$LAMBDA_NAME" --timeout "$LAMBDA_TIMEOUT" --environment "$ENV_VARS" --layers "arn:aws:lambda:$DYNAMODB_REGION:464622532012:layer:Datadog-Python36:8"
}
###########################################################
# Initialize environment
###########################################################
AWS_ROLE=$(read_var AWS_ROLE .env)
AIRNOW_API_KEYS=$(read_var AIRNOW_API_KEYS .env)
AIR_QUALITY_API_URL=$(read_var AIR_QUALITY_API_URL .env)
DYNAMODB_ENDPOINT=$(read_var DYNAMODB_ENDPOINT .env)
DYNAMODB_REGION=$(read_var DYNAMODB_REGION .env)
DYNAMODB_AQI_TABLE=$(read_var DYNAMODB_AQI_TABLE .env)
DATADOG_API_KEY=$(read_var DATADOG_API_KEY .env)
DATADOG_APP_KEY=$(read_var DATADOG_APP_KEY .env)
###########################################################
# Initialize AWS environment
###########################################################
if ! aws dynamodb list-tables --profile "$AWS_CREDENTIALS_PROFILE" 2>&1 | grep -q "$DYNAMODB_AQI_TABLE"
then
aws dynamodb create-table --profile "$AWS_CREDENTIALS_PROFILE" --table-name "$DYNAMODB_AQI_TABLE" --attribute-definitions AttributeName=PartitionKey,AttributeType=S --key-schema AttributeName=PartitionKey,KeyType=HASH --provisioned-throughput ReadCapacityUnits=1,WriteCapacityUnits=1 || exit
aws dynamodb wait table-exists --profile "$AWS_CREDENTIALS_PROFILE" --table-name "$DYNAMODB_AQI_TABLE"
fi
###########################################################
# Cleanup and rebuild artifacts past builds
###########################################################
rm AirQuality_*.zip
rm -rf build
LAMBDA_FOLDER=aqi_GET
LAMBDA_NAME=AirQuality_aqi_GET
build $LAMBDA_FOLDER $LAMBDA_NAME
LAMBDA_FOLDER=inbound_POST
LAMBDA_NAME=AirQuality_inbound_POST
build $LAMBDA_FOLDER $LAMBDA_NAME
###########################################################
# Deploy Lambdas
###########################################################
LAMBDA_NAME=AirQuality_aqi_GET
LAMBDA_TIMEOUT=15
ENV_VARS='{"Variables":{"AIRNOW_API_KEYS":"'$AIRNOW_API_KEYS'","DYNAMODB_ENDPOINT":"'$DYNAMODB_ENDPOINT'","DYNAMODB_REGION":"'$DYNAMODB_REGION'","DYNAMODB_AQI_TABLE":"'$DYNAMODB_AQI_TABLE'","DATADOG_API_KEY":"'$DATADOG_API_KEY'","DATADOG_APP_KEY":"'$DATADOG_APP_KEY'"}}'
deploy $LAMBDA_NAME $LAMBDA_TIMEOUT "$ENV_VARS"
LAMBDA_NAME=AirQuality_inbound_POST
LAMBDA_TIMEOUT=20
ENV_VARS='{"Variables":{"AIR_QUALITY_API_URL":"'$AIR_QUALITY_API_URL'","DATADOG_API_KEY":"'$DATADOG_API_KEY'","DATADOG_APP_KEY":"'$DATADOG_APP_KEY'"}}'
deploy $LAMBDA_NAME $LAMBDA_TIMEOUT "$ENV_VARS"
###########################################################
# Trigger E2E Tests
###########################################################
# TODO: needs to be udpated with GitHub Actions