Skip to content

Commit 5038e0f

Browse files
committed
setup the application
0 parents  commit 5038e0f

File tree

11 files changed

+145
-0
lines changed

11 files changed

+145
-0
lines changed

.gitignore

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
*.swp
2+
package-lock.json
3+
__pycache__
4+
.pytest_cache
5+
.env
6+
.venv
7+
*.egg-info
8+
9+
# CDK asset staging directory
10+
.cdk.staging
11+
cdk.out
12+
.idea/

README.md

Whitespace-only changes.

app.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from aws_cdk import core
2+
3+
from cdk_ecs_example.cdk_ecs_example_stack import CdkEcsExampleStack
4+
5+
app = core.App()
6+
CdkEcsExampleStack(app, "CdkEcsExample")
7+
8+
app.synth()

cdk.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"app": "python3 app.py",
3+
"context": {
4+
"@aws-cdk/core:enableStackNameDuplicates": "true",
5+
"aws-cdk:enableDiffNoFail": "true",
6+
"@aws-cdk/core:stackRelativeExports": "true",
7+
"@aws-cdk/aws-ecr-assets:dockerIgnoreSupport": true,
8+
"@aws-cdk/aws-secretsmanager:parseOwnedSecretName": true,
9+
"@aws-cdk/aws-kms:defaultKeyPolicies": true,
10+
"@aws-cdk/aws-s3:grantWriteWithoutAcl": true
11+
}
12+
}

cdk_ecs_example/__init__.py

Whitespace-only changes.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import aws_cdk.aws_ec2 as ec2
2+
import aws_cdk.aws_ecs as ecs
3+
import aws_cdk.aws_ecs_patterns as ecs_patterns
4+
from aws_cdk import core
5+
import os
6+
7+
8+
class CdkEcsExampleStack(core.Stack):
9+
def __init__(self, scope: core.Construct, construct_id: str, **kwargs) -> None:
10+
super().__init__(scope, construct_id, **kwargs)
11+
vpc = ec2.Vpc(self, "EcsVpc", max_azs=2, nat_gateways=0)
12+
vpc.add_s3_endpoint("S3Endpoint")
13+
vpc.add_interface_endpoint(
14+
"EcrDockerEndpoint", service=ec2.InterfaceVpcEndpointAwsService.ECR_DOCKER
15+
)
16+
vpc.add_interface_endpoint(
17+
"EcrEndpoint", service=ec2.InterfaceVpcEndpointAwsService.ECR
18+
)
19+
vpc.add_interface_endpoint(
20+
"CloudWatchLogsEndpoint",
21+
service=ec2.InterfaceVpcEndpointAwsService.CLOUDWATCH_LOGS,
22+
)
23+
cluster = ecs.Cluster(self, "EcsCluster", vpc=vpc)
24+
task_definition = ecs.FargateTaskDefinition(
25+
self,
26+
"DemoServiceTask",
27+
family="DemoServiceTask",
28+
volumes=[
29+
ecs.Volume(
30+
name="test-volume",
31+
host=ecs.Host(source_path=os.path.join(os.getcwd(), "service")),
32+
)
33+
],
34+
)
35+
36+
image = ecs.ContainerImage.from_asset("service")
37+
38+
container = task_definition.add_container("app", image=image)
39+
container.add_port_mappings(ecs.PortMapping(container_port=8080))
40+
41+
# Add mount point for the container
42+
container.add_mount_points(
43+
ecs.MountPoint(
44+
container_path="/app", source_volume="test-volume", read_only=True
45+
),
46+
)
47+
48+
ecs_patterns.ApplicationLoadBalancedFargateService(
49+
self,
50+
"DemoService",
51+
cluster=cluster,
52+
desired_count=2,
53+
task_definition=task_definition,
54+
)

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-e .

service/Dockerfile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
FROM python:3.9-slim
2+
WORKDIR /app
3+
COPY requirements.txt .
4+
RUN pip install -r requirements.txt
5+
6+
COPY main.py ./app
7+
8+
EXPOSE 8080
9+
ENV FLASK_APP=app
10+
11+
CMD ["python", "main.py"]

service/main.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from flask import Flask, request
2+
3+
app = Flask(__name__)
4+
5+
6+
@app.route("/")
7+
def hello_world():
8+
return "Hello, LocalStack!"
9+
10+
11+
# GET route
12+
@app.route("/get", methods=["GET"])
13+
def get_example():
14+
return "This is a GET request example."
15+
16+
17+
# POST route
18+
@app.route("/post", methods=["POST"])
19+
def post_example():
20+
data = request.json
21+
return f"This is a POST request example. Received data: {data}"
22+
23+
24+
if __name__ == "__main__":
25+
app.run(port=8080, host="0.0.0.0", debug=True)

service/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
flask

0 commit comments

Comments
 (0)