Skip to content

Commit cbcacb1

Browse files
authored
Migrate to using Terraform Deployment (#46)
* Add Terraform project to create ECR repositories in the operations account * Add Terraform project to create the ECS service and other resources * Update Travis-CI configuration to deploy using Terraform * Update documentation
1 parent b9563fe commit cbcacb1

29 files changed

+472
-873
lines changed

.gitignore

+12
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
# Local .terraform directories
2+
**/.terraform/*
3+
4+
# .tfstate files
5+
*.tfstate
6+
*.tfstate.*
7+
8+
# .tfvars files
9+
*.tfvars
10+
11+
# Crash logs
12+
crash.log
113

214
# Created by https://www.gitignore.io/api/java,gradle,eclipse,netbeans,intellij
315

.travis.yml

+86-67
Large diffs are not rendered by default.

README.md

+104-12
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,30 @@
44
[![Known Vulnerabilities](https://sonarcloud.io/api/project_badges/measure?project=spring-boot-java-base&metric=vulnerabilities)](https://sonarcloud.io/api/project_badges/measure?project=spring-boot-java-base&metric=vulnerabilities)
55
# Spring Boot Java Base
66

7-
## How tos
7+
## Build & Test
8+
9+
This project uses gradle and uses the default tasks to compile and run unit tests.
810

9-
### Build & Test
1011
```bash
1112
./gradlew clean assemble check
1213
```
1314

14-
### How to: Build and run locally on Docker
15-
1. `./gradlew clean assemble check docker`
16-
2. `docker run -e SPRING_PROFILES_ACTIVE=localhost -p 8080:8080 -i -t spring-boot-java-base`
15+
### Build and run locally on Docker
16+
1. Build the docker container
17+
```bash
18+
./gradlew clean assemble check docker
19+
```
20+
2. Run the docker container
21+
```bash
22+
docker run -e SPRING_PROFILES_ACTIVE=localhost -p 8080:8080 -i -t spring-boot-java-base
23+
```
24+
25+
### Build production equivalent container
26+
```bash
27+
./gradlew clean assemble check docker dockerTag -PTAG=$(git rev-parse --verify HEAD --short) -PREPOSITORY_URI=${DOCKER_REPO}${IMAGE_NAME}
28+
```
1729

18-
#### Debug / Profiling
30+
### Profiling
1931
To debug the container locally, the `JAVA_OPTS` environment variable can be provided when running the container.
2032
```bash
2133
docker run -p 8080:8080 -i -t -e JAVA_OPTS="-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005" spring-boot-java-base
@@ -27,12 +39,92 @@ New relic can be enabled by providing the following `JAVA_OPTS` environment vari
2739
docker run -p 8080:8080 -i -t -e JAVA_OPTS="-javaagent:newrelic/newrelic.jar -Dnewrelic.environment=development -Dnewrelic.config.file=newrelic/newrelic.yml" spring-boot-java-base
2840
```
2941

30-
### How to: Build production equivalent container
31-
```bash
32-
./gradlew clean assemble check docker dockerTag -PTAG=$(git rev-parse --verify HEAD --short) -PREPOSITORY_URI=${DOCKER_REPO}${IMAGE_NAME}
42+
## Deployment
43+
44+
This project uses Terraform and the AWS CLI to deploy the service to the BNC ECS Cluster. To have the CI/CD pipeline deploy a service which has be deployed using a fork of this project you can follow the instructions below.
45+
46+
### Terraform ECS Workspaces
47+
48+
By default Terraform will not create the required workspaces. Before setting up the deployment in the CI environment, ensure you have created all of the appropriate workspaces.
49+
50+
The default workspaces for BNC are:
51+
* development
52+
* production
53+
54+
To create the workspaces run the following commands:
55+
```
56+
cd deployment/terraform/ecs-service
57+
terraform workspace new production
58+
terraform workspace new development
3359
```
3460

35-
## For more tasks run
36-
```bash
37-
./gradlew tasks
61+
### Setting up Travis-CI deployment
62+
63+
1. Encrypt the following global environment variables using the Travis-CI CLI.
64+
```
65+
AWS_ACCESS_KEY_ID=
66+
AWS_SECRET_ACCESS_KEY=
67+
AWS_DEFAULT_REGION=
68+
KMS_KEY_ID=
69+
ROLE_ARN=
70+
STATE_S3_BUCKET=
71+
STATE_DYNAMODB_TABLE=
72+
KEY=<The project key for the ECR repository>, e.g bnc/<team>/ecr/<service-name>
73+
SERVICE_KEY=<The project key for ECS service>, e.g bnc/<team>/<workspace>/ecs/<service-name>
74+
OPERATIONS_ROLE_ARN=
75+
DEVELOPMENT_ROLE_ARN=
76+
PRODUCTION_ROLE_ARN=
77+
SPLUNK_URL=
78+
```
79+
80+
2. Encrypt the following environment variables for the development deployment:
81+
```
82+
TF_WORKSPACE=
83+
SPLUNK_TOKEN=
84+
```
85+
86+
### Deployment to development ECS cluster
87+
88+
#### Setup AWS Credentials
89+
90+
1. Setup the AWS profile using `aws configure --profile bnc-terraform`. The credentials can be retrieved using `terraform output` command in the terraform-techemy-master project if you have this setup.
91+
92+
#### Terraform ECR Project
93+
94+
1. cd deployment/terraform/ecr
95+
96+
2. Copy `backend.tfvars.example` to `backend.tfvars`.
97+
98+
3. Fill out the `backend.tfvars`
99+
100+
4. Run `terraform init "-backend-config=backend.tfvars"`.
101+
102+
5. Copy `master.tfvars.example` to `master.tfvars`.
103+
104+
6. Fill in the `master.tfvars` with the correct values.
105+
106+
7. Now the project is fully setup and you will have the ability to run [terraform commands](https://www.terraform.io/docs/commands/index.html).
107+
```
108+
terraform plan "-var-file=master.tfvars"
109+
```
110+
111+
#### Terraform ECS Project
112+
113+
1. cd deployment/terraform/ecs-service
114+
115+
2. Copy `backend.tfvars.example` to `backend.tfvars`.
116+
117+
3. Fill out the `backend.tfvars`
118+
119+
4. Run `terraform init "-backend-config=backend.tfvars"`.
120+
121+
5. Copy `master.tfvars.example` to `master.tfvars`.
122+
123+
6. Fill in the `master.tfvars` with the correct values.
124+
125+
7. Select the development work space `terraform workspace select development`
126+
127+
8. Now the project is fully setup and you will have the ability to run [terraform commands](https://www.terraform.io/docs/commands/index.html).
128+
```
129+
terraform plan "-var-file=master.tfvars"
38130
```

build.gradle

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
plugins {
22
id 'eclipse'
33
id 'idea'
4-
id "org.sonarqube" version "2.7" apply false
4+
id "org.sonarqube" version "2.7.1" apply false
55
id "com.github.spotbugs" version "1.6.9" apply false
66
id "com.bnc.gradle.travis-ci-versioner" version "1.0.6"
77
}
88

99
ext {
1010
awsAccessKeyId = properties.containsKey('AWS_ACCESS_KEY_ID') ? AWS_ACCESS_KEY_ID : System.getenv('AWS_ACCESS_KEY_ID')
1111
awsSecretAccessKey = properties.containsKey('AWS_SECRET_ACCESS_KEY') ? AWS_SECRET_ACCESS_KEY : System.getenv('AWS_SECRET_ACCESS_KEY')
12+
awsSessionToken = System.getenv('AWS_SESSION_TOKEN')
1213
}
1314

1415
travisVersioner {
@@ -27,15 +28,23 @@ allprojects {
2728
sourceCompatibility = JavaVersion.VERSION_11
2829
targetCompatibility = JavaVersion.VERSION_11
2930
compileJava.options.encoding = "UTF-8"
31+
compileTestJava.options.encoding = "UTF-8"
3032

3133
repositories {
3234
mavenCentral()
3335
jcenter()
3436
maven {
37+
// This repository is only used for Brave New Coin artifacts. It is not required to build this project.
3538
url "s3://artifact.bravenewcoin.com/maven/release"
39+
content {
40+
includeGroup "com.bnc"
41+
}
3642
credentials(AwsCredentials) {
3743
accessKey "${awsAccessKeyId}"
3844
secretKey "${awsSecretAccessKey}"
45+
if (awsSessionToken) {
46+
sessionToken "${awsSessionToken}"
47+
}
3948
}
4049
}
4150
}

client/build.gradle

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ jar {
99
publishing {
1010
publications {
1111
mavenJava(MavenPublication) {
12+
groupId = "com.bnc.${rootProject.name}"
1213
from components.java
1314
}
1415
}
@@ -19,6 +20,9 @@ publishing {
1920
credentials(AwsCredentials) {
2021
accessKey "${awsAccessKeyId}"
2122
secretKey "${awsSecretAccessKey}"
23+
if (awsSessionToken) {
24+
sessionToken "${awsSessionToken}"
25+
}
2226
}
2327
}
2428
}

0 commit comments

Comments
 (0)