Skip to content

Commit 442a3ce

Browse files
authored
Merge pull request #441 from gsingh04/develop
Refactoring solution build
2 parents 3051d5c + e9a85b7 commit 442a3ce

33 files changed

+8397
-684
lines changed

.github/workflows/run-unit-test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
runs-on: ubuntu-latest
1616
strategy:
1717
matrix:
18-
node-version: [16.x]
18+
node-version: [14.x]
1919
steps:
2020
- uses: actions/checkout@v3
2121
- uses: actions/setup-node@v3

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
**/dist
33
**/global-s3-assets
44
**/regional-s3-assets
5+
**/staging
56
**/open-source
67
**/.zip
78
**/tmp
@@ -14,6 +15,7 @@
1415
**/coverage
1516
**/coverage-reports
1617
**/.nyc_output
18+
**/mock-*
1719

1820
# misc
1921
**/npm-debug.log

NOTICE.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ This software includes third party software subject to the following copyrights:
2626
@types/uuid under the Massachusetts Institute of Technology (MIT) license
2727
@typescript-eslint/eslint-plugin under the Massachusetts Institute of Technology (MIT) license
2828
@typescript-eslint/parser under the BSD 2-Clause license
29+
adm-zip under the Massachusetts Institute of Technology (MIT) license
2930
aws-cdk under the Apache License 2.0
3031
aws-sdk under the Apache License 2.0
3132
axios under the Massachusetts Institute of Technology (MIT) license

README.md

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@
1010
- [Customizing the Solution](#customizing-the-solution)
1111
- [Prerequisites for Customization](#prerequisites-for-customization)
1212
- [1. Clone the repository](#1-clone-the-repository)
13-
- [2. Declare environment variables](#2-declare-environment-variables)
14-
- [Unit Test](#unit-test)
15-
- [Build](#build)
16-
- [Deploy](#deploy)
13+
- [2. Unit Test](#2-unit-test)
14+
- [3. Build & Deploy](#3-build-and-deploy)
1715
- [Collection of operational metrics](#collection-of-operational-metrics)
1816
- [External Contributors](#external-contributors)
1917
- [License](#license)
@@ -46,7 +44,7 @@ In addition to the AWS Solutions Constructs, the solution uses AWS CDK directly
4644
## Prerequisites for Customization
4745

4846
- [AWS Command Line Interface](https://aws.amazon.com/cli/)
49-
- Node.js 16.x or later
47+
- Node.js 14.x
5048

5149
### 1. Clone the repository
5250

@@ -56,39 +54,30 @@ cd serverless-image-handler
5654
export MAIN_DIRECTORY=$PWD
5755
```
5856

59-
### 2. Declare environment variables
6057

61-
```bash
62-
export REGION=aws-region-code # the AWS region to launch the solution (e.g. us-east-1)
63-
export BUCKET_PREFIX=my-bucket-name # the bucket prefix, randomized name recommended
64-
export BUCKET_NAME=$BUCKET_PREFIX-$REGION # the bucket name where customized code will reside
65-
export SOLUTION_NAME=my-solution-name # the solution name
66-
export VERSION=my-version # version number for the customized code
67-
```
68-
69-
## Unit Test
58+
### 2. Unit Test
7059

7160
After making changes, run unit tests to make sure added customization passes the tests:
7261

7362
```bash
7463
cd $MAIN_DIRECTORY/deployment
75-
chmod +x run-unit-tests.sh
76-
./run-unit-tests.sh
64+
chmod +x run-unit-tests.sh && ./run-unit-tests.sh
7765
```
7866

79-
## Build
80-
67+
### 3. Build and Deploy
8168
```bash
82-
cd $MAIN_DIRECTORY/deployment
83-
chmod +x build-s3-dist.sh
84-
./build-s3-dist.sh $BUCKET_PREFIX $SOLUTION_NAME $VERSION
69+
cd $MAIN_DIRECTORY/source/constructs
70+
npm run clean:install
71+
overrideWarningsEnabled=false npx cdk bootstrap --profile <PROFILE_NAME>
72+
overrideWarningsEnabled=false npx cdk deploy\
73+
--parameters DeployDemoUIParameter=Yes\
74+
--parameters SourceBucketsParameter=<MY_BUCKET>\
75+
--profile <PROFILE_NAME>
8576
```
8677

87-
## Deploy
88-
89-
- Deploy the distributable to the Amazon S3 bucket in your account. Make sure you are uploading the files in `deployment/global-s3-assets` and `deployment/regional-s3-assets` to `$BUCKET_NAME/$SOLUTION_NAME/$VERSION`.
90-
- Get the link of the solution template uploaded to your Amazon S3 bucket.
91-
- Deploy the solution to your account by launching a new AWS CloudFormation stack using the link of the solution template in Amazon S3.
78+
_Note:_
79+
- **MY_BUCKET**: name of an existing bucket in your account
80+
- **PROFILE_NAME**: name of an AWS CLI profile that has appropriate credentials for deploying in your preferred region
9281

9382
# Collection of operational metrics
9483

@@ -116,6 +105,5 @@ This solution collects anonymous operational metrics to help AWS improve the qua
116105

117106
# License
118107

119-
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
120-
121-
SPDX-License-Identifier: Apache-2.0
108+
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
109+
SPDX-License-Identifier: Apache-2.0

buildspec.yml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,16 @@ phases:
66
nodejs: 16
77
pre_build:
88
commands:
9-
- npm install -g [email protected]
9+
- n 14
1010
- echo "Installing dependencies and executing unit tests - `pwd`"
1111
- cd deployment && chmod +x ./run-unit-tests.sh && ./run-unit-tests.sh
1212
- echo "Installing dependencies and executing unit tests completed `date`"
1313
build:
1414
commands:
1515
- echo "Starting build `date` in `pwd`"
16-
- chmod +x ./build-s3-dist.sh && ./build-s3-dist.sh $DIST_OUTPUT_BUCKET $SOLUTION_NAME $VERSION
16+
- chmod +x ./build-s3-dist.sh && ./build-s3-dist.sh
1717
- echo "Build completed `date`"
18-
- echo "Starting open-source-dist `date` in `pwd`"
19-
- chmod +x ./build-open-source-dist.sh && ./build-open-source-dist.sh $SOLUTION_NAME
20-
- echo "Open Source Dist completed `date`"
18+
- mkdir ./open-source && touch ./open-source/NoOp
2119
post_build:
2220
commands:
2321
- echo "Retrieving next stage buildspec `date` in `pwd`"

deployment/build-open-source-dist.sh

Lines changed: 0 additions & 78 deletions
This file was deleted.

deployment/build-s3-dist.sh

Lines changed: 38 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,46 @@
11
#!/bin/bash
2-
#
3-
# This assumes all of the OS-level configuration has been completed and git repo has already been cloned
4-
#
5-
# This script should be run from the repo's deployment directory
6-
# cd deployment
7-
# ./build-s3-dist.sh source-bucket-base-name trademarked-solution-name version-code
8-
#
9-
# Paramenters:
10-
# - source-bucket-base-name: Name for the S3 bucket location where the template will source the Lambda
11-
# code from. The template will append '-[region_name]' to this bucket name.
12-
# For example: ./build-s3-dist.sh solutions my-solution v1.0.0
13-
# The template will then expect the source code to be located in the solutions-[region_name] bucket
14-
#
15-
# - trademarked-solution-name: name of the solution for consistency
16-
#
17-
# - version-code: version of the package
2+
# The script is for aws-solutions internal purposes only
3+
4+
[ "$DEBUG" == 'true' ] && set -x
5+
set -e
186

197
# Check to see if input has been provided:
20-
if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ]; then
21-
echo "Please provide the base source bucket name, trademark approved solution name and version where the lambda code will eventually reside."
22-
echo "For example: ./build-s3-dist.sh solutions trademarked-solution-name v1.0.0"
8+
if [ -z "$DIST_OUTPUT_BUCKET" ] || [ -z "$SOLUTION_NAME" ] || [ -z "$VERSION" ]; then
9+
echo "Please provide the base source bucket name, trademark approved solution name and version through environment variables"
2310
exit 1
2411
fi
2512

26-
# Exit immediately if a command exits with a non-zero status.
27-
set -e
28-
29-
# Get reference for all important folders
30-
template_dir="$PWD"
31-
template_dist_dir="$template_dir/global-s3-assets"
32-
build_dist_dir="$template_dir/regional-s3-assets"
33-
source_dir="$template_dir/../source"
13+
function headline(){
14+
echo "------------------------------------------------------------------------------"
15+
echo "$1"
16+
echo "------------------------------------------------------------------------------"
17+
}
18+
19+
headline "[Init] Setting up paths and variables"
20+
deployment_dir="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
21+
staging_dist_dir="$deployment_dir/staging"
22+
template_dist_dir="$deployment_dir/global-s3-assets"
23+
build_dist_dir="$deployment_dir/regional-s3-assets"
24+
source_dir="$deployment_dir/../source"
3425
cdk_source_dir="$source_dir/constructs"
3526

36-
echo "------------------------------------------------------------------------------"
37-
echo "[Init] Clean old dist folders"
38-
echo "------------------------------------------------------------------------------"
39-
echo "rm -rf $template_dist_dir"
40-
rm -rf $template_dist_dir
41-
echo "mkdir -p $template_dist_dir"
42-
mkdir -p $template_dist_dir
43-
echo "rm -rf $build_dist_dir"
44-
rm -rf $build_dist_dir
45-
echo "mkdir -p $build_dist_dir"
46-
mkdir -p $build_dist_dir
47-
48-
echo "------------------------------------------------------------------------------"
49-
echo "Synthesize the CDK project into a CloudFormation template"
50-
echo "------------------------------------------------------------------------------"
51-
export SOLUTION_BUCKET_NAME_PLACEHOLDER=$1
52-
export SOLUTION_NAME_PLACEHOLDER=$2
53-
export SOLUTION_VERSION_PLACEHOLDER=$3
54-
export overrideWarningsEnabled=false
55-
56-
cd $cdk_source_dir
57-
npm run clean
58-
npm install
59-
node_modules/aws-cdk/bin/cdk synth --asset-metadata false --path-metadata false >$template_dist_dir/$2.template
60-
61-
declare -a lambda_packages=(
62-
"image-handler"
63-
"custom-resource"
64-
)
65-
66-
for lambda_package in "${lambda_packages[@]}"; do
67-
echo "------------------------------------------------------------------------------"
68-
echo "Building Lambda package: $lambda_package"
69-
echo "------------------------------------------------------------------------------"
70-
cd $source_dir/$lambda_package
71-
npm run package
72-
# Check the result of the package step and exit if a failure is identified
73-
if [ $? -eq 0 ]; then
74-
echo "Package for $lambda_package built successfully"
75-
else
76-
echo "******************************************************************************"
77-
echo "Lambda package build FAILED for $lambda_package"
78-
echo "******************************************************************************"
79-
exit 1
80-
fi
81-
mv dist/package.zip $build_dist_dir/$lambda_package.zip
82-
rm -rf dist
83-
done
84-
85-
echo "------------------------------------------------------------------------------"
86-
echo "Package Serverless Image Handler Demo UI"
87-
echo "------------------------------------------------------------------------------"
88-
mkdir $build_dist_dir/demo-ui/
89-
cp -r $source_dir/demo-ui/** $build_dist_dir/demo-ui/
90-
91-
echo "------------------------------------------------------------------------------"
92-
echo "[Create] Console manifest"
93-
echo "------------------------------------------------------------------------------"
94-
cd $source_dir/demo-ui
95-
manifest=($(find * -type f ! -iname ".DS_Store"))
96-
manifest_json=$(
97-
IFS=,
98-
printf "%s" "${manifest[*]}"
99-
)
100-
echo "{\"files\":[\"$manifest_json\"]}" | sed 's/,/","/g' >>$build_dist_dir/demo-ui-manifest.json
27+
headline "[Init] Clean old folders"
28+
rm -rf "$staging_dist_dir"
29+
mkdir -p "$staging_dist_dir"
30+
rm -rf "$template_dist_dir"
31+
mkdir -p "$template_dist_dir"
32+
rm -rf "$build_dist_dir"
33+
mkdir -p "$build_dist_dir"
34+
35+
headline "[Build] Synthesize cdk template and assets"
36+
cd "$cdk_source_dir"
37+
npm run clean:install
38+
overrideWarningsEnabled=false npx cdk synth --quiet --asset-metadata false --path-metadata --output="$staging_dist_dir"
39+
cd "$staging_dist_dir"
40+
rm tree.json manifest.json cdk.out ./*.assets.json
41+
cp "$staging_dist_dir"/*.template.json "$template_dist_dir"/"$SOLUTION_NAME".template
42+
rm ./*.template.json
43+
44+
headline "[Package] Generate public assets for lambda and ui"
45+
cd "$deployment_dir"/cdk-solution-helper/asset-packager && npm ci
46+
npx ts-node ./index "$staging_dist_dir" "$build_dist_dir"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
## CDK Solution Helper
2+
3+
**For aws-solutions internal purposes only**
4+
5+
`cdk-solution-helper` runs on solution internal pipeline and makes needed artifact modifications to support 1-click deployment using solution CloudFormation template.
6+
Additionally, it packages templates and lambda binaries and prepares them to be staged on the solution buckets.

0 commit comments

Comments
 (0)