Build Spec and CDK Initialized
structure
auto-trading-bot
├── build-spec
│ └── docker_build_buildspec.yml
├── s3source
│ └── docker-build.zip
└── app.py
The docker build and deployment
Build Spec
First deployment in CodePipeline
./build-spec/docker_build_builspec.yml
version: 0.2
phases:
pre_build:
commands:
- echo Logging into Amazon ECR
- aws ecr get-login-password --region $AWS_DEFAULT_REGION | docker login --username AWS --password-stdin $ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com
build:
commands:
- echo Entered the post_build phase...
- echo Build completed on `date`
- docker build -t ${IMAGE_NAME} docker-build/
- docker tag $IMAGE_NAME:latest $ECR_URI:latest
- docker push $ECR_URI:latest
You need to add pos_build
commands to update function code after the first deployment to let Lambda main function update for any changing in GitHub commits.
./build-spec/docker_build_builspec.yml
post_build:
commands:
- echo Update function code ...
- aws lambda update-function-code --function-name $FUNCTION_NAME --image-uri $ECR_URI:latest --query "FunctionName"
AWS S3 Source
auto-trading-bot
├── build-spec
│ └── docker_build_buildspec.yml
├── docker-build
│ ├── app.py
│ ├── Dockerfile
│ ├── send_message.py
│ └── utils.py
Create the s3source
folder contains docker-build.zip
which includes all files in both folders build-spec
and docker-build
in zip file.
CDK intialized
Project created
Create your working folder to deploy the application in CLI terminal, activate Python environment and install required packages.
# Create working folder and CDK initialized
mkdir auto-trading-bot
cd auto-trading-bot
cdk init app --language python
# Activate Python environment
source .venv/bin/activate
# Install required packages for CDK
pip install -r requirements.txt
Configuration
CDK main function
Fill in the information prepared in Build related services for AWS.
auto-trading-bot/app.py
# Named the general namespace on your choice
namespace = <YOUR_NAMESPACE>
# IP list protection for webhook received (provided by TradingView)
webhook_ip_allowed_list = ["52.89.214.238", "34.212.75.30", "54.218.53.128", "52.32.178.7"]
# Configuration for message notification when placing the order
message_config = {
"message_webhook_id": <YOUR_DISCORD_WEBHOOK_ID>,
"message_webhook_token": <YOUR_DISCORD_WEBHOOK_TOKEN>
}
# Parameter format
props = {
"SECRET_NAME": <YOUR_SECRET_NAME>,
"REGION_NAME": <REGION_NAME>,
"GIT_HUB_OWNER": <YOUR_GITHUB_OWNER_NAME>
"GIT_HUB_REPO": <YOUR_REPO_NAME>,
"GIT_HUB_BRANCH": "main",
"WEBHOOK_SSM_NAME": "webhook_ip_list",
"WEBHOOK_IP_ALLOWED_LIST": webhook_ip_allowed_list,
"MESSAGE_NAME": "discord_notification",
"MESSAGE_CONFIG": message_config,
"namespace": namespace,
"function_name": f"{namespace}-TradingBotLambda",
"custom_auth_function_name": f"{namespace}-CustomAuth",
"bucket_name": f"{namespace.lower()}-trading-bot-bucket",
"docker_build_project_name": f"{namespace}-DockerBuild",
"pipeline_name": f"{namespace}-Pipeline"
}
I used nested stack to split the deployment into 02 parts since we need to wait for docker image deployed on ECR at the first time deployment.
auto-trading-bot/app.py
app = cdk.App()
# Create ECR and deployment docker image first time
base_ecr = AutoTradingBotEcrStack(app, f"{props["namespace"]}-BotEcr", props)
# Create REST API Gateway, Lambda main function and Lambda Authorizer
rest_api_execution = AutoTradingBotRestApiStack(app, f"{props["namespace"]}-RestApi", base_ecr.outputs)
rest_api_execution.add_dependency(base_ecr)
app.synth()