cdk-workshop-02
cdk-workshop-02
python
CDK projectcdk-workshop-02/cdk_workshop_02_stack.py
, import these librariesfrom aws_cdk import (
Stack,
aws_ecs as ecs,
aws_ecs_patterns as ecsp,
aws_apigateway as apigateway
)
from constructs import Construct
cdk-workshop-02/cdk_workshop_02_stack.py
, declare a ECS cluster together with Application Load Balancer in the __init__()
function # Declare a Load Balancer Fargate
lb_fargate_service = ecsp.ApplicationLoadBalancedFargateService(
self,
"Workshop02-MyWebServer",
task_image_options=ecsp.ApplicationLoadBalancedTaskImageOptions(
image=ecs.ContainerImage.from_registry("nginxdemos/hello")
),
public_load_balancer=True,
desired_count=3
)
ecsp.ApplicationLoadBalancedFargateService
will create the following resources:
nginxdemos/hello
image from Dockerhub# Define API Gateway
api = apigateway.RestApi(self, "ProxyToLBECS")
# Get the DNS value of the Application Load Balancer
lb = lb_fargate_service.load_balancer
lb_dns = lb_fargate_service.load_balancer.load_balancer_dns_name
# Add resource and method for proxy request
proxy = api.root.add_resource("ecs")
proxy.add_method("GET", apigateway.HttpIntegration(f"http://{lb_dns}"))
You can read more about the methods and properties of ApplicationLoadBalancedFargateService at the official CDK documentation of AWS (Python)
cdk-workshop-02/cdk_workshop_02_stack.py
from aws_cdk import (
Stack,
aws_ecs as ecs,
aws_ecs_patterns as ecsp,
aws_apigateway as apigateway
)
from constructs import Construct
class CdkWorkshop02Stack(Stack):
def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
super().__init__(scope, construct_id, **kwargs)
# Declare a Load Balancer Fargate
lb_fargate_service = ecsp.ApplicationLoadBalancedFargateService(
self,
"Workshop02-MyWebServer",
task_image_options=ecsp.ApplicationLoadBalancedTaskImageOptions(
image=ecs.ContainerImage.from_registry("nginxdemos/hello")),
public_load_balancer=True,
desired_count=3
)
# Define API Gateway
api = apigateway.RestApi(self, "ProxyToLBECS")
# Get the DNS value of the Application Load Balancer
lb = lb_fargate_service.load_balancer
lb_dns = lb_fargate_service.load_balancer.load_balancer_dns_name
# Add resource and method for proxy request
proxy = api.root.add_resource("ecs")
proxy.add_method("GET", apigateway.HttpIntegration(f"http://{lb_dns}"))
Before we deploy, we must
cdk-workshop-02
directory, because our EC2 instance has 2 versions of python, we have to isolate them to make sure there aren’t conflicts between python’s packages.requirements.txt
virtualenv
/home/ec2-user
pip3.9 install virtualenv
cdk-workshop-02
venv
venv/bin/activate
virtualenv venv
source venv/bin/activate
requirements.txt
pip install -r requirements.txt
cdk diff
cdk boostrap
cdk deploy
Choose y
to continue
Access the link to the API Gateway (ProxyToLBECSEndpoint
), add the path /ecs
. You will be able to access the webserver deployed in ECS.
To verify that the ALB works properly, hit refresh a couple of times, and you will see the target of the website change. In the previous section, we set the parameter desired_count=3
, so there will be 3 containers being deployed in parallel behind the ALB. You will notice the change in the field Server address
between the refreshes.
If you have reached this state, congratulations on finishing the first part of the CDK basic 2 Workshop. In the next section, we will use CDK to deploy a Lambda function behind an API Gateway resource.