Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add IaC support #4

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions iac/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
.terraform
cdktf.out
cdktf.log
*terraform.*.tfstate*

.classpath
.project
.idea
.settings
.vscode
*.iml
target/
.gradle/
build/
src/main/java/imports/
src/main/resources/imports/
117 changes: 117 additions & 0 deletions iac/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
## Use CDKTF to deploy the infrastructure and the app

### Install CDKTF

* The [Terraform CLI](https://developer.hashicorp.com/terraform/tutorials/aws-get-started/install-cli) (1.2+).
* [Node.js](https://nodejs.org/en) and npm v16+.
* Java [OpenJDK v17](https://openjdk.java.net/) and [Gradle](https://gradle.org/install/)

__Note:__ The latest CDKTF for Java is using Gradle instead Maven, which provides faster synthesizing.


For more details, read the [Install CDKTF tutorial](https://developer.hashicorp.com/terraform/tutorials/cdktf/cdktf-install).

### Build the container images

The images need to be built and stored in a registry. For this application, you can build different types of images. The following commands provide an example:

```bash
# Assuming from the root directory of the project
ROOT_DIR=$(pwd)

cd $ROOT_DIR/services/audit
gcloud builds submit . --machine-type E2-HIGHCPU-32 --config cloudbuild-native.yaml

cd $ROOT_DIR/services/faulty
gcloud builds submit . --machine-type E2-HIGHCPU-32 --config cloudbuild.yaml --substitutions=_TYPE=native

cd $ROOT_DIR/services/bff
gcloud builds submit . --machine-type E2-HIGHCPU-32 --config cloudbuild.yaml --substitutions=_TYPE=native

cd $ROOT_DIR/services/reference
gcloud builds submit . --machine-type E2-HIGHCPU-32 --config cloudbuild.yaml --substitutions=_TYPE=native

cd $ROOT_DIR/services/quotes
gcloud builds submit . --machine-type E2-HIGHCPU-32 --config cloudbuild-native.yaml
```

### Deploy the infrastructure and the app

You can use different backends to manage the Terraform state. Here is an example using the GCS backend.

```bash
export PROJECT_ID=[Use your GCP project Id here]
export GCS_BACKEND_BUCKET_NAME=${PROJECT_ID}-cdktf-state
gcloud storage buckets create gs://${GCS_BACKEND_BUCKET_NAME}
```

Use the CDKTF CLI to deploy. Notice you can pass in various parameters for the deployment.
```bash
cd $ROOT_DIR/iac
cdktf deploy application-dev \
--var='auditImageName=audit-native' \
--var='referenceImageName=reference-native' \
--var='bffImageName=bff-native' \
--var='faultyImageName=faulty-native' \
--var='quotesImageName=quotes-native' \
--auto-approve
```

Wait for a few minutes and check the results. If the command runs successfully, you should see output like the following:
```terminal
...
bff-dev
bff-service-loadbalancer = http://34.36.147.241
bff-service-url = https://bff-service-uzog2g4wga-uc.a.run.app
...
```

Run the following command to get a JSON output:
```bash
curl http://[load balancer ip]/quotes
```
If you get an error like `Recv failure`, wait a few minutes and try again.

### Destroy

To destroy the deployment, you can run the following commands:
```bash
export PROJECT_ID=[Use your GCP project Id here]
export GCS_BACKEND_BUCKET_NAME=${PROJECT_ID}-cdktf-state
cdktf destroy application-dev --auto-approve
```

## Use scripts
Alternatively, you can use the scripts in this directory to automate some of the tasks.

1. Run the following script to build the container images. You can update the script for different types of images:
```bash
./build-images.sh
```

2. Use Cloud Build to deploy or destroy the resources:

Grant the required access to your Cloud Build service account:

```bash
CLOUDBUILD_SA="$(gcloud projects describe $PROJECT_ID \
--format 'value(projectNumber)')@cloudbuild.gserviceaccount.com"
gcloud projects add-iam-policy-binding $PROJECT_ID \
--member serviceAccount:$CLOUDBUILD_SA --role roles/editor
gcloud projects add-iam-policy-binding $PROJECT_ID \
--member serviceAccount:$CLOUDBUILD_SA --role roles/secretmanager.admin
gcloud projects add-iam-policy-binding $PROJECT_ID \
--member serviceAccount:$CLOUDBUILD_SA --role roles/servicenetworking.networksAdmin
```

Deploy the resources:
```bash
gcloud builds submit --config=cloudbuild.yaml \
--substitutions=_CDKTF_TYPE="deploy"
```

Destroy the resources:
```bash
gcloud builds submit --config=cloudbuild.yaml \
--substitutions=_CDKTF_TYPE="destroy"
```
22 changes: 22 additions & 0 deletions iac/build-images.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash

# exit if a command returns a non-zero exit code and also print the commands and their args as they are executed.
set -e -x

# Assuming from the root directory of the project
ROOT_DIR=`dirname $(realpath $0)`/../

cd $ROOT_DIR/services/audit
gcloud builds submit . --machine-type E2-HIGHCPU-32 --config cloudbuild-native.yaml

cd $ROOT_DIR/services/faulty
gcloud builds submit . --machine-type E2-HIGHCPU-32 --config cloudbuild.yaml --substitutions=_TYPE=native

cd $ROOT_DIR/services/bff
gcloud builds submit . --machine-type E2-HIGHCPU-32 --config cloudbuild.yaml --substitutions=_TYPE=native

cd $ROOT_DIR/services/reference
gcloud builds submit . --machine-type E2-HIGHCPU-32 --config cloudbuild.yaml --substitutions=_TYPE=native

cd $ROOT_DIR/services/quotes
gcloud builds submit . --machine-type E2-HIGHCPU-32 --config cloudbuild-native.yaml
55 changes: 55 additions & 0 deletions iac/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
plugins {
id "application"
id "java-library"
id "maven-publish"
}
apply plugin : "java"
ext {
javaMainClass = "com.mycompany.app.Main"
}

application {
mainClassName = javaMainClass
}

repositories {
mavenLocal()
maven {
url = uri("https://repo.maven.apache.org/maven2/")
}
}

dependencies {
implementation group: 'com.hashicorp', name: 'cdktf-provider-docker', version: '9.0.0'
implementation group: 'com.hashicorp', name: 'cdktf-provider-google', version: '9.0.2'
implementation group: 'com.hashicorp', name: 'cdktf', version: '0.18.0'
implementation group: 'com.hashicorp', name: 'cdktf-provider-random', version: '9.0.0'
implementation group: 'software.constructs', name: 'constructs', version: '10.2.70'
testImplementation "junit:junit:4.13.2"
testImplementation "org.junit.jupiter:junit-jupiter:5.8.0"
}

group = "com.mycompany.app"
version = "0.1"
description = "IaC"

publishing {
publications {
maven(MavenPublication) {
from(components.java)
}
}
}

tasks.withType(JavaCompile) {
options.encoding = "UTF-8"
}

tasks.withType(Javadoc) {
options.encoding = "UTF-8"
}

test {
useJUnit()
useJUnitPlatform()
}
11 changes: 11 additions & 0 deletions iac/cdktf.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"language": "java",
"app": "./gradlew run",
"projectId": "122ffb2f-c6f1-4d7b-ac80-a352d36b5fc4",
"sendCrashReports": "false",
"codeMakerOutput": "src/main/java/imports",
"terraformProviders": [],
"terraformModules": [],
"context": {
}
}
31 changes: 31 additions & 0 deletions iac/cdktf.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/bash

# exit if a command returns a non-zero exit code and also print the commands and their args as they are executed.
set -e -x

# Download and install required tools.
npm install -g cdktf-cli

export GCS_BACKEND_BUCKET_NAME=${PROJECT_ID}-cdktf-state
gcloud storage buckets create gs://${GCS_BACKEND_BUCKET_NAME} 2>/dev/null || true

case $CDKTF_TYPE in
destroy)
cdktf destroy application-dev --auto-approve
;;
deploy)
cdktf deploy application-dev \
--var='auditImageName=audit-native' \
--var='referenceImageName=reference-native' \
--var='bffImageName=bff-native' \
--var='faultyImageName=faulty-native' \
--var='quotesImageName=quotes-native' \
--auto-approve
;;
*)
cdktf synth *
;;
esac



7 changes: 7 additions & 0 deletions iac/cloudbuild.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
steps:
- name: 'hashicorp/jsii-terraform'
entrypoint: sh
args: ["-c","chmod +x *.sh && ./cdktf.sh"]
env:
- 'CDKTF_TYPE=$_CDKTF_TYPE'
- 'PROJECT_ID=$PROJECT_ID'
1 change: 1 addition & 0 deletions iac/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.gradle.jvmargs=-Xmx4096m
Binary file added iac/gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
7 changes: 7 additions & 0 deletions iac/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Loading