diff --git a/instrument-aws/instrument-ecs-cdk/.gitignore b/instrument-aws/instrument-ecs-cdk/.gitignore new file mode 100644 index 00000000..91cb228e --- /dev/null +++ b/instrument-aws/instrument-ecs-cdk/.gitignore @@ -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/ diff --git a/instrument-aws/instrument-ecs-cdk/README.md b/instrument-aws/instrument-ecs-cdk/README.md new file mode 100644 index 00000000..8da46149 --- /dev/null +++ b/instrument-aws/instrument-ecs-cdk/README.md @@ -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) \ No newline at end of file diff --git a/instrument-aws/instrument-ecs-cdk/buildAndDeploy.sh b/instrument-aws/instrument-ecs-cdk/buildAndDeploy.sh new file mode 100644 index 00000000..0bf7de1e --- /dev/null +++ b/instrument-aws/instrument-ecs-cdk/buildAndDeploy.sh @@ -0,0 +1,3 @@ +#!/bin/sh +set -e +mvn clean package && cdk deploy \ No newline at end of file diff --git a/instrument-aws/instrument-ecs-cdk/buildAndDeployDontAsk.sh b/instrument-aws/instrument-ecs-cdk/buildAndDeployDontAsk.sh new file mode 100644 index 00000000..68605941 --- /dev/null +++ b/instrument-aws/instrument-ecs-cdk/buildAndDeployDontAsk.sh @@ -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 \ No newline at end of file diff --git a/instrument-aws/instrument-ecs-cdk/cdk.json b/instrument-aws/instrument-ecs-cdk/cdk.json new file mode 100644 index 00000000..c0fc001c --- /dev/null +++ b/instrument-aws/instrument-ecs-cdk/cdk.json @@ -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 + } +} \ No newline at end of file diff --git a/instrument-aws/instrument-ecs-cdk/destroy.sh b/instrument-aws/instrument-ecs-cdk/destroy.sh new file mode 100644 index 00000000..8f70dd1b --- /dev/null +++ b/instrument-aws/instrument-ecs-cdk/destroy.sh @@ -0,0 +1,3 @@ +#!/bin/sh +set -e +cdk destroy \ No newline at end of file diff --git a/instrument-aws/instrument-ecs-cdk/pom.xml b/instrument-aws/instrument-ecs-cdk/pom.xml new file mode 100644 index 00000000..ffeac295 --- /dev/null +++ b/instrument-aws/instrument-ecs-cdk/pom.xml @@ -0,0 +1,82 @@ + + + 4.0.0 + + + jomu + instrument-aws + 0.0.1-SNAPSHOT + + + instrument-ecs-cdk + + + + + org.codehaus.mojo + exec-maven-plugin + 3.0.0 + + jomu.instrument.CDKApp + + + + + + + + + software.amazon.awscdk + aws-cdk-lib + 2.79.1 + + + + software.amazon.awscdk + apigatewayv2-alpha + 2.66.1-alpha.0 + + + software.amazon.awscdk + apigatewayv2-integrations-alpha + 2.66.1-alpha.0 + + + software.constructs + constructs + 10.0.0 + + + org.eclipse + yasson + 2.0.4 + + + org.junit.jupiter + junit-jupiter-api + 5.9.2 + test + + + org.junit.jupiter + junit-jupiter-engine + 5.9.2 + test + + + org.assertj + assertj-core + 3.24.2 + test + + + + UTF-8 + 17 + 17 + 17 + + \ No newline at end of file diff --git a/instrument-aws/instrument-ecs-cdk/src/main/java/jomu/instrument/CDKApp.java b/instrument-aws/instrument-ecs-cdk/src/main/java/jomu/instrument/CDKApp.java new file mode 100644 index 00000000..42dd2d27 --- /dev/null +++ b/instrument-aws/instrument-ecs-cdk/src/main/java/jomu/instrument/CDKApp.java @@ -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(); + + } +} diff --git a/instrument-aws/instrument-ecs-cdk/src/main/java/jomu/instrument/ECSFargateLoadBalancedStack.java b/instrument-aws/instrument-ecs-cdk/src/main/java/jomu/instrument/ECSFargateLoadBalancedStack.java new file mode 100644 index 00000000..b5fdbcef --- /dev/null +++ b/instrument-aws/instrument-ecs-cdk/src/main/java/jomu/instrument/ECSFargateLoadBalancedStack.java @@ -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"); + + } +} diff --git a/instrument-aws/pom.xml b/instrument-aws/pom.xml index 6d76d1e4..85664bbc 100644 --- a/instrument-aws/pom.xml +++ b/instrument-aws/pom.xml @@ -12,6 +12,7 @@ instrument-s3handler instrument-s3handler-cdk + instrument-ecs-cdk instrument-s3handler-st