Skip to content

Commit

Permalink
ecs cdk
Browse files Browse the repository at this point in the history
  • Loading branch information
Jim O'Mulloy committed Jun 29, 2023
1 parent a1e2678 commit e63f79f
Show file tree
Hide file tree
Showing 10 changed files with 289 additions and 0 deletions.
43 changes: 43 additions & 0 deletions instrument-aws/instrument-ecs-cdk/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#Maven
target/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
release.properties

# Eclipse
.project
.classpath
.settings/
bin/

# IntelliJ
.idea
*.ipr
*.iml
*.iws

# NetBeans
nb-configuration.xml

# Visual Studio Code
.vscode
.factorypath

# OSX
.DS_Store

# Vim
*.swp
*.swo

# patch
*.orig
*.rej

# Local environment
.env

# CDK
cdk.out/
/cdk.out/
31 changes: 31 additions & 0 deletions instrument-aws/instrument-ecs-cdk/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Slightly Streamlined AWS Cloud Development Kit (CDK) Boilerplate

This is a blank, slightly streamlined, project for Java development with CDK.

You will find the maven command in `cdk.json` file.

It is a [Maven](https://maven.apache.org/) based project, so you can open this project with any Maven compatible Java IDE to build and run tests.

## Installation

1. Install [AWS CDK CLI](https://docs.aws.amazon.com/cdk/latest/guide/getting_started.html)
2. [`cdk boostrap --profile YOUR_AWS_PROFILE`](https://docs.aws.amazon.com/cdk/latest/guide/bootstrapping.html)

## Useful commands

* `mvn package` compile and run tests
* `cdk ls` list all stacks in the app
* `cdk synth` emits the synthesized CloudFormation template
* `cdk deploy` deploy this stack to your default AWS account/region
* `cdk diff` compare deployed stack with current state
* `cdk docs` open CDK documentation

Enjoy!

## in action

[![Infrastructure as Java Code (IaJC): Setting AWS System Manager Parameter](https://i.ytimg.com/vi/eTG7EV1ThqQ/mqdefault.jpg)](https://www.youtube.com/embed/eTG7EV1ThqQ?rel=0)



See you at: [airhacks.live](https://airhacks.live)
3 changes: 3 additions & 0 deletions instrument-aws/instrument-ecs-cdk/buildAndDeploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh
set -e
mvn clean package && cdk deploy
9 changes: 9 additions & 0 deletions instrument-aws/instrument-ecs-cdk/buildAndDeployDontAsk.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/sh
set -e
cd ..
cd ..
mvn clean package
cd instrument-aws
cd instrument-s3-handler-cdk
mvn package
cdk deploy --all --require-approval=never
16 changes: 16 additions & 0 deletions instrument-aws/instrument-ecs-cdk/cdk.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"app": "mvn -e -q compile exec:java",
"watch":{
"include":[
"../instrument-s3handler/target/function.zip",
"../instrument-s3handler/src/main/resources/application.properties"
]
},
"context": {
"@aws-cdk/aws-apigateway:usagePlanKeyOrderInsensitiveId": true,
"@aws-cdk/core:stackRelativeExports": true,
"@aws-cdk/aws-rds:lowercaseDbIdentifier": true,
"@aws-cdk/aws-lambda:recognizeVersionProps": true,
"@aws-cdk/aws-cloudfront:defaultSecurityPolicyTLSv1.2_2021": true
}
}
3 changes: 3 additions & 0 deletions instrument-aws/instrument-ecs-cdk/destroy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh
set -e
cdk destroy
82 changes: 82 additions & 0 deletions instrument-aws/instrument-ecs-cdk/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?xml version="1.0" encoding="UTF-8"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>jomu</groupId>
<artifactId>instrument-aws</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>

<artifactId>instrument-ecs-cdk</artifactId>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<mainClass>jomu.instrument.CDKApp</mainClass>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<!-- AWS Cloud Development Kit -->
<dependency>
<groupId>software.amazon.awscdk</groupId>
<artifactId>aws-cdk-lib</artifactId>
<version>2.79.1</version>
</dependency>
<!-- alpha versions for HttpApi gateway integration-->
<dependency>
<groupId>software.amazon.awscdk</groupId>
<artifactId>apigatewayv2-alpha</artifactId>
<version>2.66.1-alpha.0</version>
</dependency>
<dependency>
<groupId>software.amazon.awscdk</groupId>
<artifactId>apigatewayv2-integrations-alpha</artifactId>
<version>2.66.1-alpha.0</version>
</dependency>
<dependency>
<groupId>software.constructs</groupId>
<artifactId>constructs</artifactId>
<version>10.0.0</version>
</dependency>
<dependency>
<groupId>org.eclipse</groupId>
<artifactId>yasson</artifactId>
<version>2.0.4</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.9.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.9.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.24.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<java.version>17</java.version>
</properties>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package jomu.instrument;

import software.amazon.awscdk.App;
import software.amazon.awscdk.Environment;
import software.amazon.awscdk.StackProps;
import software.amazon.awscdk.Tags;

public class CDKApp {

static StackProps createStackProperties() {
var account = System.getenv("CDK_DEPLOY_ACCOUNT");
var region = System.getenv("CDK_DEPLOY_REGION");

if (account == null)
return StackProps.builder().build();

var environment = Environment.builder().account(account).region(region).build();
return StackProps.builder().env(environment).build();
}

public static void main(final String[] args) {

var app = new App();
var appName = "instrument-ecs";

Tags.of(app).add("project", "instrument");
Tags.of(app).add("environment", "development");
Tags.of(app).add("application", appName);

var stackProps = createStackProperties();

new ECSFargateLoadBalancedStack(app, appName);

app.synth();

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package jomu.instrument;

import software.amazon.awscdk.Stack;
import software.amazon.awscdk.StackProps;
import software.amazon.awscdk.services.ec2.Peer;
import software.amazon.awscdk.services.ec2.Port;
import software.amazon.awscdk.services.ec2.Vpc;
import software.amazon.awscdk.services.ec2.VpcProps;
import software.amazon.awscdk.services.ecs.Cluster;
import software.amazon.awscdk.services.ecs.ClusterProps;
import software.amazon.awscdk.services.ecs.ContainerImage;
import software.amazon.awscdk.services.ecs.patterns.ApplicationLoadBalancedFargateService;
import software.amazon.awscdk.services.ecs.patterns.ApplicationLoadBalancedTaskImageOptions;
import software.constructs.Construct;

public class ECSFargateLoadBalancedStack extends Stack {

public ECSFargateLoadBalancedStack(final Construct parent, final String id) {
this(parent, id, null);
}

public ECSFargateLoadBalancedStack(final Construct parent, final String id, final StackProps props) {
super(parent, id, props);

// Create VPC with a AZ limit of two.
Vpc vpc = new Vpc(this, "InstrumentEcsVpc", VpcProps.builder().maxAzs(2).build());

// Create the ECS Service
Cluster cluster = new Cluster(this, "InstrumentEcsCluster", ClusterProps.builder().vpc(vpc).build());

// Use the ECS Network Load Balanced Fargate Service construct to create a ECS
// service
// NetworkLoadBalancedFargateService fargateService = new NetworkLoadBalancedFargateService(this,
// "InstrumentFargateService",
// NetworkLoadBalancedFargateServiceProps.builder().cluster(cluster)
// .taskImageOptions(NetworkLoadBalancedTaskImageOptions.builder()
// .image(ContainerImage
// .fromRegistry("942706091699.dkr.ecr.eu-west-2.amazonaws.com/instrument:latest"))
// .build())
// .build());

// Create a load-balanced Fargate service and make it public
ApplicationLoadBalancedFargateService fargateService = ApplicationLoadBalancedFargateService.Builder
.create(this, "InstrumentFargateService").cluster(cluster) // Required
.cpu(512) // Default is 256
.desiredCount(2) // Default is 1
.taskImageOptions(ApplicationLoadBalancedTaskImageOptions.builder()
.image(ContainerImage
.fromRegistry("942706091699.dkr.ecr.eu-west-2.amazonaws.com/instrument:latest"))
.build())
.memoryLimitMiB(2048) // Default is 512
.publicLoadBalancer(true) // Default is true
.build();

// Open port 80 inbound to IPs within VPC to allow network load balancer to
// connect to the service
// fargateService.getService().getConnections().getSecurityGroups().get(0)
// .addIngressRule(Peer.ipv4(vpc.getVpcCidrBlock()), Port.tcp(80), "allow http
// inbound from vpc");
fargateService.getService().getConnections().getSecurityGroups().get(0)
.addIngressRule(Peer.ipv4(vpc.getVpcCidrBlock()), Port.tcp(9080), "allow http inbound from vpc");

}
}
1 change: 1 addition & 0 deletions instrument-aws/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<modules>
<module>instrument-s3handler</module>
<module>instrument-s3handler-cdk</module>
<module>instrument-ecs-cdk</module>
<module>instrument-s3handler-st</module>
</modules>
</project>

0 comments on commit e63f79f

Please sign in to comment.