Skip to content

Commit db155d2

Browse files
piradeepkrix0rrr
authored andcommitted
feat: fargate service using a local image (aws-samples#33)
1 parent 4f0954d commit db155d2

File tree

7 files changed

+111
-0
lines changed

7 files changed

+111
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"app": "node index"
3+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import ec2 = require('@aws-cdk/aws-ec2');
2+
import ecs = require('@aws-cdk/aws-ecs');
3+
import cdk = require('@aws-cdk/cdk');
4+
import path = require('path');
5+
6+
const app = new cdk.App();
7+
const stack = new cdk.Stack(app, 'FargateServiceWithLocalImage');
8+
9+
// Create VPC and Fargate Cluster
10+
// NOTE: Limit AZs to avoid reaching resource quotas
11+
const vpc = new ec2.VpcNetwork(stack, 'MyVpc', { maxAZs: 2 });
12+
const cluster = new ecs.Cluster(stack, 'Cluster', { vpc });
13+
14+
// Instantiate Fargate Service with a cluster and a local image that gets
15+
// uploaded to an S3 staging bucket prior to being uploaded to ECR.
16+
// A new repository is created in ECR and the Fargate service is created
17+
// with the image from ECR.
18+
new ecs.LoadBalancedFargateService(stack, "FargateService", {
19+
cluster,
20+
image: ecs.ContainerImage.fromAsset(stack, "local-image" , {
21+
directory: path.join(__dirname, 'local-image')
22+
})
23+
});
24+
25+
app.run();
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Use an official Python runtime as a parent image
2+
FROM python:3.4-alpine
3+
4+
# Set the working directory to /app
5+
WORKDIR /app
6+
7+
# Copy the current directory contents into the container at /app
8+
COPY . /app
9+
10+
# Install any needed packages specified in requirements.txt
11+
RUN pip install --trusted-host pypi.python.org -r requirements.txt
12+
13+
# Make port 80 available to the world outside this container
14+
EXPOSE 80
15+
16+
# Define environment variable
17+
ENV NAME World
18+
19+
# Run app.py when the container launches
20+
CMD ["python", "app.py"]
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from flask import Flask
2+
import os
3+
import socket
4+
5+
app = Flask(__name__)
6+
7+
@app.route("/")
8+
def hello():
9+
html = "<h3>Hello {name}!</h3>" \
10+
"<b>Hostname:</b> {hostname}<br/>"
11+
return html.format(name=os.getenv("NAME", "world"), hostname=socket.gethostname())
12+
13+
if __name__ == "__main__":
14+
app.run(host='0.0.0.0', port=80)
15+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
flask
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "fargate-service-with-local-image",
3+
"version": "0.25.1",
4+
"description": "Running a load balanced service on Fargate with an image loaded locally",
5+
"private": true,
6+
"scripts": {
7+
"build": "tsc",
8+
"watch": "tsc -w",
9+
"cdk": "cdk"
10+
},
11+
"author": {
12+
"name": "Amazon Web Services",
13+
"url": "https://aws.amazon.com",
14+
"organization": true
15+
},
16+
"license": "Apache-2.0",
17+
"devDependencies": {
18+
"@types/node": "^8.10.38",
19+
"typescript": "^3.2.4"
20+
},
21+
"dependencies": {
22+
"@aws-cdk/aws-ec2": "*",
23+
"@aws-cdk/aws-ecs": "*",
24+
"@aws-cdk/cdk": "*"
25+
}
26+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"compilerOptions": {
3+
"target":"ES2018",
4+
"module": "commonjs",
5+
"lib": ["es2016", "es2017.object", "es2017.string"],
6+
"strict": true,
7+
"noImplicitAny": true,
8+
"strictNullChecks": true,
9+
"noImplicitThis": true,
10+
"alwaysStrict": true,
11+
"noUnusedLocals": true,
12+
"noUnusedParameters": true,
13+
"noImplicitReturns": true,
14+
"noFallthroughCasesInSwitch": false,
15+
"inlineSourceMap": true,
16+
"inlineSources": true,
17+
"experimentalDecorators": true,
18+
"strictPropertyInitialization":false
19+
}
20+
}
21+

0 commit comments

Comments
 (0)