Skip to content

Commit

Permalink
feat: fargate service using a local image (aws-samples#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
piradeepk authored and rix0rrr committed Apr 18, 2019
1 parent 4f0954d commit db155d2
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 0 deletions.
3 changes: 3 additions & 0 deletions typescript/ecs/fargate-service-with-local-image/cdk.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"app": "node index"
}
25 changes: 25 additions & 0 deletions typescript/ecs/fargate-service-with-local-image/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import ec2 = require('@aws-cdk/aws-ec2');
import ecs = require('@aws-cdk/aws-ecs');
import cdk = require('@aws-cdk/cdk');
import path = require('path');

const app = new cdk.App();
const stack = new cdk.Stack(app, 'FargateServiceWithLocalImage');

// Create VPC and Fargate Cluster
// NOTE: Limit AZs to avoid reaching resource quotas
const vpc = new ec2.VpcNetwork(stack, 'MyVpc', { maxAZs: 2 });
const cluster = new ecs.Cluster(stack, 'Cluster', { vpc });

// Instantiate Fargate Service with a cluster and a local image that gets
// uploaded to an S3 staging bucket prior to being uploaded to ECR.
// A new repository is created in ECR and the Fargate service is created
// with the image from ECR.
new ecs.LoadBalancedFargateService(stack, "FargateService", {
cluster,
image: ecs.ContainerImage.fromAsset(stack, "local-image" , {
directory: path.join(__dirname, 'local-image')
})
});

app.run();
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Use an official Python runtime as a parent image
FROM python:3.4-alpine

# Set the working directory to /app
WORKDIR /app

# Copy the current directory contents into the container at /app
COPY . /app

# Install any needed packages specified in requirements.txt
RUN pip install --trusted-host pypi.python.org -r requirements.txt

# Make port 80 available to the world outside this container
EXPOSE 80

# Define environment variable
ENV NAME World

# Run app.py when the container launches
CMD ["python", "app.py"]
15 changes: 15 additions & 0 deletions typescript/ecs/fargate-service-with-local-image/local-image/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from flask import Flask
import os
import socket

app = Flask(__name__)

@app.route("/")
def hello():
html = "<h3>Hello {name}!</h3>" \
"<b>Hostname:</b> {hostname}<br/>"
return html.format(name=os.getenv("NAME", "world"), hostname=socket.gethostname())

if __name__ == "__main__":
app.run(host='0.0.0.0', port=80)

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
flask
26 changes: 26 additions & 0 deletions typescript/ecs/fargate-service-with-local-image/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "fargate-service-with-local-image",
"version": "0.25.1",
"description": "Running a load balanced service on Fargate with an image loaded locally",
"private": true,
"scripts": {
"build": "tsc",
"watch": "tsc -w",
"cdk": "cdk"
},
"author": {
"name": "Amazon Web Services",
"url": "https://aws.amazon.com",
"organization": true
},
"license": "Apache-2.0",
"devDependencies": {
"@types/node": "^8.10.38",
"typescript": "^3.2.4"
},
"dependencies": {
"@aws-cdk/aws-ec2": "*",
"@aws-cdk/aws-ecs": "*",
"@aws-cdk/cdk": "*"
}
}
21 changes: 21 additions & 0 deletions typescript/ecs/fargate-service-with-local-image/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"compilerOptions": {
"target":"ES2018",
"module": "commonjs",
"lib": ["es2016", "es2017.object", "es2017.string"],
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"noImplicitThis": true,
"alwaysStrict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": false,
"inlineSourceMap": true,
"inlineSources": true,
"experimentalDecorators": true,
"strictPropertyInitialization":false
}
}

0 comments on commit db155d2

Please sign in to comment.