When working with Serverless Apps, many components are needed to be created in the Cloud. An example of these components are:
- Lambda functions for compute
- API gateway for API hosting
- CDN for Request Routing, CORS and HTTP Caching.
- S3 Storage for Angular Applications
- S3 Storage for private app data
- Rewrites rules (for Angular Deployments)
- Minimal user credentails for Deployment
- IAM Roles
An CDK (Infrastructure as code - IaC) is provided in order to automate Infrastructure creation.
The IaC source code is public and can be modified by the user if needed.
Prerequisites:
Create AWS Access Key and Secret Key for Deployment
For this, create a User (and AccessKeys) in AWS Console with the Following IAM permission policy (or just use an Administrator User)
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"sts:AssumeRole"
],
"Resource": [
"arn:aws:iam::*:role/cdk-*"
]
}
]
}
Run from the command line (CMD.exe):
//Navigate to a folder of your preference (ex: cd c:\temp\serverless)
npm i aws-cdk -g
git clone https://github.com/genexuslabs/gx-aws-cdk-samples.git
cd gx-aws-cdk-samples/aws-serverless
npm install
npm run build
//Set you AWS Credentials
SET AWS_ACCESS_KEY_ID=AKIAWJJTXXXXXXXX
SET AWS_SECRET_ACCESS_KEY=8YA1XhWpDKCCyHlUfeSciXXXXXX
SET AWS_DEFAULT_REGION=us-east-1
cdk bootstrap aws://ACCOUNT-NUMBER
/AWS-REGION
//Set you AWS Credentials
SET AWS_ACCESS_KEY_ID=AKIAWJJTXXXXXXXX
SET AWS_SECRET_ACCESS_KEY=8YA1XhWpDKCCyHlUfeSciXXXXXX
SET AWS_DEFAULT_REGION=us-east-1
cdk deploy --all --require-approval never
- cdk bootstrap aws://
ACCOUNT-NUMBER
/AWS-REGION
- ACCOUNT-NUMBER: Is your AWS Account Number.
- AWS-Region: Is the AWS Region where you want to create de Stack. Must match "AWS_DEFAULT_REGION".
- Example:
- cdk bootstrap aws://123456789012/us-east-1
- More info
Available Parameters (for cdk deploy command)
- appname
- stage
- Stage Name for URL. Usually: v1, v2,v3
- domain (optional)
- Cloudfront Domain Name for Http Endpoints
- certificateARN (optional)
- AWS Certificate ARN required when using a domain.
- memory (optional)
- Memory Size of Lambda Compute service (defaults: 1024MB)
You can change parameters in file cdk.json located in the root directory.
{
"context": {
"appname": "GXServerlessApp",
"stage": "test"
}
}
Other Lambda predefined settings:
- Lambda Function Timeout: 30 seconds
- Lambda Runtime: Java Corretto 11
After executing the above commands, Cloud Infrastructure is created and basic outputs are printed to the Console. Those outputs are needed in order to Deploy From GeneXus IDE or MsBuild Tasks.
Sample output:
MyGeneXusServerlessApp.ServerlessAppaccessKey18458E8E = AKIAWJJT4NXXXX63
MyGeneXusServerlessApp.ServerlessAppapiName899AA174 = myTest
MyGeneXusServerlessApp.ServerlessAppapiUrl13CD1FA5 = https://xxxxxxxx.execute-api.us-east-1.amazonaws.com/test/
MyGeneXusServerlessApp.ServerlessAppmyTestapigwEndpoint9036D366 = https://yrotw9qh4a.execute-api.us-east-1.amazonaws.com/test/
MyGeneXusServerlessApp.ServerlessApproleARN7A858545 = arn:aws:iam::XXXXXXXXXX:role/MyGeneXusServerlessApp-ServerlessAppmyTestla-S4JXVWBX7Q4K
MyGeneXusServerlessApp.ServerlessAppsecretKeyEE0B667A = EDINc64KBcy4ipzGXXXXXXSHF0MCeU24IOJUX
MyGeneXusServerlessApp.ServerlessAppstorageBucket7C6914DC = mygenexusserverlessapp-serverlessappmytestb-r03ed8rdbdit
MyGeneXusServerlessApp.ServerlessAppwebsiteBucketB6BBF9D7 = mygenexusserverlessapp-serverlessappmytestb-1qiheafyokggb
This output can also be seen from AWS Cloudformation Console:
Plase note that this information is needed by the GeneXus Deploy Tool/Serverless Properties.