├── .dockerignore ├── .gitignore ├── LICENSE ├── build.gradle ├── deploy ├── docker │ ├── docker-compose-prod.yml │ ├── docker-compose-qa.yml │ └── docker-compose.yml └── terraform │ ├── datasource.tf │ ├── main.tf │ ├── outputs.tf │ ├── providers.tf │ ├── security.tf │ └── userdata.tpl ├── diagram ├── tutorial-diagram-level-1.jpg ├── tutorial-diagram-level-2.jpg └── tutorial-diagram-level-3.jpg ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── readme.md ├── settings.gradle └── src ├── main ├── java │ └── com │ │ └── maraujo │ │ └── contactbook │ │ ├── ProjetoModeloApplication.java │ │ ├── controller │ │ ├── ContactbookController.java │ │ ├── handler │ │ │ └── ControllerAdvice.java │ │ └── request │ │ │ └── ContactRequest.java │ │ ├── entity │ │ └── Contact.java │ │ ├── repository │ │ └── ContactbookRepository.java │ │ └── service │ │ ├── ContactbookServiceImpl.java │ │ ├── client │ │ ├── ViaCepClient.java │ │ └── response │ │ │ └── AddressResponse.java │ │ └── interfaces │ │ └── ContactbookService.java └── resources │ ├── application-prod.yml │ ├── application-qa.yml │ ├── application.yml │ └── db │ └── migration │ └── V1_1__create_table_contactbook.sql └── test └── java └── com └── maraujo └── contactbook └── ProjetoModeloApplicationTests.java /.dockerignore: -------------------------------------------------------------------------------- 1 | terraform 2 | .git 3 | diagram -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | .gradle 3 | build/ 4 | !gradle/wrapper/gradle-wrapper.jar 5 | !**/src/main/**/build/ 6 | !**/src/test/**/build/ 7 | 8 | ### STS ### 9 | .apt_generated 10 | .classpath 11 | .factorypath 12 | .project 13 | .settings 14 | .springBeans 15 | .sts4-cache 16 | bin/ 17 | !**/src/main/**/bin/ 18 | !**/src/test/**/bin/ 19 | 20 | ### IntelliJ IDEA ### 21 | .idea 22 | *.iws 23 | *.iml 24 | *.ipr 25 | out/ 26 | !**/src/main/**/out/ 27 | !**/src/test/**/out/ 28 | 29 | ### NetBeans ### 30 | /nbproject/private/ 31 | /nbbuild/ 32 | /dist/ 33 | /nbdist/ 34 | /.nb-gradle/ 35 | 36 | ### VS Code ### 37 | .vscode/ 38 | 39 | # Local terraform directories 40 | .terraform/ 41 | 42 | # State files 43 | *.tfstate 44 | .tfstate. 45 | 46 | # Crash log files 47 | *.log 48 | 49 | # Sensitive data 50 | *.tfvars -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Thiago Pereira de Oliveira 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'org.springframework.boot' version '2.7.4' 3 | id 'io.spring.dependency-management' version '1.0.14.RELEASE' 4 | id 'java' 5 | } 6 | 7 | group = 'com.maraujo' 8 | version = '0.0.1-SNAPSHOT' 9 | sourceCompatibility = '11' 10 | 11 | configurations { 12 | compileOnly { 13 | extendsFrom annotationProcessor 14 | } 15 | } 16 | 17 | repositories { 18 | mavenCentral() 19 | } 20 | 21 | ext { 22 | set('springCloudVersion', "2021.0.4") 23 | } 24 | 25 | dependencies { 26 | implementation 'org.springframework.boot:spring-boot-starter-web' 27 | implementation 'org.springframework.boot:spring-boot-starter-actuator' 28 | implementation 'org.springdoc:springdoc-openapi-ui:1.6.11' 29 | implementation 'org.flywaydb:flyway-core' 30 | implementation 'org.springframework.boot:spring-boot-starter-data-jpa' 31 | runtimeOnly 'org.postgresql:postgresql' 32 | 33 | implementation 'org.springframework.cloud:spring-cloud-starter-openfeign' 34 | 35 | compileOnly 'org.projectlombok:lombok' 36 | developmentOnly 'org.springframework.boot:spring-boot-devtools' 37 | annotationProcessor 'org.projectlombok:lombok' 38 | testImplementation 'org.springframework.boot:spring-boot-starter-test' 39 | } 40 | 41 | dependencyManagement { 42 | imports { 43 | mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}" 44 | } 45 | } 46 | 47 | 48 | tasks.named('test') { 49 | useJUnitPlatform() 50 | } 51 | 52 | 53 | bootBuildImage{ 54 | imageName = "contact-book-api" 55 | } -------------------------------------------------------------------------------- /deploy/docker/docker-compose-prod.yml: -------------------------------------------------------------------------------- 1 | version: '3' 2 | 3 | services: 4 | postgres-db: 5 | container_name: postgres-db 6 | image: postgres 7 | environment: 8 | POSTGRES_USER: "admin" 9 | POSTGRES_PASSWORD: "admin" 10 | POSTGRES_DB: "contactbook" 11 | TZ: "America/Sao_Paulo" 12 | ports: 13 | - "15432:5432" 14 | networks: 15 | - docker-network 16 | restart: always 17 | 18 | contact-book: 19 | container_name: contact-book 20 | image: marceloaraujo14/contact-book-api:latest 21 | environment: 22 | SERVER_PORT: 3000 23 | SPRING_PROFILES_ACTIVE: prod 24 | TZ: "America/Sao_Paulo" 25 | ports: 26 | - "80:3000" 27 | depends_on: 28 | - postgres-db 29 | networks: 30 | - docker-network 31 | deploy: 32 | resources: 33 | limits: 34 | memory: 700M 35 | reservations: 36 | memory: 700M 37 | restart: always 38 | 39 | networks: 40 | docker-network: 41 | driver: bridge -------------------------------------------------------------------------------- /deploy/docker/docker-compose-qa.yml: -------------------------------------------------------------------------------- 1 | version: '3' 2 | 3 | services: 4 | postgres-db: 5 | container_name: postgres-db 6 | image: postgres 7 | environment: 8 | POSTGRES_USER: "admin" 9 | POSTGRES_PASSWORD: "admin" 10 | POSTGRES_DB: "contactbook" 11 | TZ: "America/Sao_Paulo" 12 | ports: 13 | - "15432:5432" 14 | networks: 15 | - docker-network 16 | restart: always 17 | 18 | contact-book: 19 | container_name: contact-book 20 | image: contact-book-api:latest 21 | environment: 22 | SERVER_PORT: 3000 23 | SPRING_PROFILES_ACTIVE: qa 24 | TZ: "America/Sao_Paulo" 25 | ports: 26 | - "80:3000" 27 | depends_on: 28 | - postgres-db 29 | networks: 30 | - docker-network 31 | restart: always 32 | 33 | networks: 34 | docker-network: 35 | driver: bridge -------------------------------------------------------------------------------- /deploy/docker/docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '3' 2 | 3 | services: 4 | postgres-db: 5 | container_name: postgres-db 6 | image: postgres 7 | environment: 8 | POSTGRES_USER: "admin" 9 | POSTGRES_PASSWORD: "admin" 10 | POSTGRES_DB: "contactbook" 11 | TZ: "America/Sao_Paulo" 12 | ports: 13 | - "15432:5432" 14 | networks: 15 | - docker-network 16 | restart: always 17 | 18 | pgadmin: 19 | container_name: pgadmin 20 | image: dpage/pgadmin4 21 | environment: 22 | PGADMIN_DEFAULT_EMAIL: "admin@gmail.com" 23 | PGADMIN_DEFAULT_PASSWORD: "admin" 24 | TZ: "America/Sao_Paulo" 25 | ports: 26 | - "16543:80" 27 | depends_on: 28 | - postgres-db 29 | networks: 30 | - docker-network 31 | restart: always 32 | 33 | networks: 34 | docker-network: 35 | driver: bridge -------------------------------------------------------------------------------- /deploy/terraform/datasource.tf: -------------------------------------------------------------------------------- 1 | data "aws_ami" "contactbook_server_ami" { 2 | most_recent = true 3 | owners = ["099720109477"] 4 | 5 | filter { 6 | name = "name" 7 | values = ["ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server-*"] 8 | } 9 | } -------------------------------------------------------------------------------- /deploy/terraform/main.tf: -------------------------------------------------------------------------------- 1 | resource "aws_vpc" "contactbook_vpc_1" { 2 | cidr_block = "10.0.0.0/16" 3 | enable_dns_hostnames = true 4 | enable_dns_support = true 5 | 6 | tags = { 7 | "Name" = "contactbook_vpc_1" 8 | } 9 | } 10 | 11 | 12 | resource "aws_subnet" "contactbook_subnet_pub_1a" { 13 | vpc_id = aws_vpc.contactbook_vpc_1.id 14 | cidr_block = "10.0.1.0/24" 15 | availability_zone = "us-east-1a" 16 | map_public_ip_on_launch = true 17 | 18 | tags = { 19 | "Name" = "contactbook_subnet_pub_1a" 20 | } 21 | 22 | } 23 | 24 | resource "aws_internet_gateway" "contactbook_igw_1a" { 25 | vpc_id = aws_vpc.contactbook_vpc_1.id 26 | 27 | tags = { 28 | "Name" = "contactbook_igw_1a" 29 | } 30 | } 31 | 32 | resource "aws_route_table" "contactbook_rtb_pub" { 33 | vpc_id = aws_vpc.contactbook_vpc_1.id 34 | 35 | tags = { 36 | "Name" = "contactbook_rtb_pub" 37 | } 38 | } 39 | 40 | resource "aws_route" "contactbook_default_rtb" { 41 | route_table_id = aws_route_table.contactbook_rtb_pub.id 42 | destination_cidr_block = "0.0.0.0/0" 43 | gateway_id = aws_internet_gateway.contactbook_igw_1a.id 44 | } 45 | 46 | resource "aws_route_table_association" "contactbook_rtba_pub_1a" { 47 | route_table_id = aws_route_table.contactbook_rtb_pub.id 48 | subnet_id = aws_subnet.contactbook_subnet_pub_1a.id 49 | } 50 | 51 | 52 | resource "aws_instance" "contactbook_ec2_inst" { 53 | instance_type = "t2.micro" 54 | ami = data.aws_ami.contactbook_server_ami.id 55 | key_name = aws_key_pair.contactbook_key.id 56 | vpc_security_group_ids = [aws_security_group.contactbook_sg.id] 57 | subnet_id = aws_subnet.contactbook_subnet_pub_1a.id 58 | user_data = file("userdata.tpl") 59 | 60 | root_block_device { 61 | volume_size = 8 62 | } 63 | 64 | tags = { 65 | "Name" = "contactbook_ec2_inst" 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /deploy/terraform/outputs.tf: -------------------------------------------------------------------------------- 1 | output "inst_ip" { 2 | value = aws_instance.contactbook_ec2_inst.public_ip 3 | } -------------------------------------------------------------------------------- /deploy/terraform/providers.tf: -------------------------------------------------------------------------------- 1 | terraform { 2 | required_providers { 3 | aws = { 4 | source = "hashicorp/aws" 5 | } 6 | } 7 | } 8 | 9 | provider "aws" { 10 | region = "us-east-1" 11 | shared_credentials_files = ["~/.aws/credentials"] 12 | profile = "contact_book_api" 13 | } -------------------------------------------------------------------------------- /deploy/terraform/security.tf: -------------------------------------------------------------------------------- 1 | 2 | resource "aws_security_group" "contactbook_sg" { 3 | name = "contactbook_sg" 4 | description = "contactbook security group" 5 | vpc_id = aws_vpc.contactbook_vpc_1.id 6 | 7 | tags = { 8 | "Name" = "contactbook_sg" 9 | } 10 | } 11 | 12 | resource "aws_security_group_rule" "public_out" { 13 | type = "egress" 14 | from_port = 0 15 | to_port = 0 16 | protocol = "-1" 17 | cidr_blocks = ["0.0.0.0/0"] 18 | 19 | security_group_id = aws_security_group.contactbook_sg.id 20 | } 21 | 22 | resource "aws_security_group_rule" "public_in_ssh" { 23 | type = "ingress" 24 | from_port = 22 25 | to_port = 22 26 | protocol = "tcp" 27 | cidr_blocks = ["0.0.0.0/0"] 28 | security_group_id = aws_security_group.contactbook_sg.id 29 | } 30 | 31 | resource "aws_key_pair" "contactbook_key" { 32 | key_name = "contactbook_key" 33 | public_key = file("~/.ssh/contactbook_key.pub") 34 | } 35 | 36 | resource "aws_security_group_rule" "public_in_http" { 37 | type = "ingress" 38 | from_port = 80 39 | to_port = 80 40 | protocol = "tcp" 41 | cidr_blocks = ["0.0.0.0/0"] 42 | security_group_id = aws_security_group.contactbook_sg.id 43 | } -------------------------------------------------------------------------------- /deploy/terraform/userdata.tpl: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | #install docker 3 | sudo apt-get update -y && 4 | sudo apt-get install -y \ 5 | apt-transport-https \ 6 | ca-certificates \ 7 | curl \ 8 | gnupg-agent \ 9 | software-properties-common && 10 | curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - && 11 | sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" && 12 | sudo apt-get update -y && 13 | sudo sudo apt-get install docker-ce docker-ce-cli containerd.io -y && 14 | sudo usermod -aG docker ubuntu 15 | sudo systemctl enable docker.service 16 | sudo systemctl enable containerd.service 17 | 18 | #install docker compose 19 | sudo apt install docker-compose -y 20 | 21 | #access user root 22 | sudo su 23 | 24 | #create project folder 25 | mkdir ~/project 26 | cd ~/project 27 | 28 | #clone your project from git repository 29 | git clone https://github.com/MarceloAraujo14/tutorial-spring-docker-terraform-aws.git 30 | cd tutorial-spring-docker-terraform-aws 31 | git checkout master 32 | git pull 33 | cd deploy/docker 34 | 35 | #run your docker compose file 36 | docker-compose -f docker-compose-prod.yml up -d 37 | -------------------------------------------------------------------------------- /diagram/tutorial-diagram-level-1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MarceloAraujo14/tutorial-spring-docker-terraform-aws/09dd851265154a079abc5bae7b3b377eb2d00a3c/diagram/tutorial-diagram-level-1.jpg -------------------------------------------------------------------------------- /diagram/tutorial-diagram-level-2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MarceloAraujo14/tutorial-spring-docker-terraform-aws/09dd851265154a079abc5bae7b3b377eb2d00a3c/diagram/tutorial-diagram-level-2.jpg -------------------------------------------------------------------------------- /diagram/tutorial-diagram-level-3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MarceloAraujo14/tutorial-spring-docker-terraform-aws/09dd851265154a079abc5bae7b3b377eb2d00a3c/diagram/tutorial-diagram-level-3.jpg -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MarceloAraujo14/tutorial-spring-docker-terraform-aws/09dd851265154a079abc5bae7b3b377eb2d00a3c/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # 4 | # Copyright © 2015-2021 the original authors. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # https://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | 19 | ############################################################################## 20 | # 21 | # Gradle start up script for POSIX generated by Gradle. 22 | # 23 | # Important for running: 24 | # 25 | # (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is 26 | # noncompliant, but you have some other compliant shell such as ksh or 27 | # bash, then to run this script, type that shell name before the whole 28 | # command line, like: 29 | # 30 | # ksh Gradle 31 | # 32 | # Busybox and similar reduced shells will NOT work, because this script 33 | # requires all of these POSIX shell features: 34 | # * functions; 35 | # * expansions «$var», «${var}», «${var:-default}», «${var+SET}», 36 | # «${var#prefix}», «${var%suffix}», and «$( cmd )»; 37 | # * compound commands having a testable exit status, especially «case»; 38 | # * various built-in commands including «command», «set», and «ulimit». 39 | # 40 | # Important for patching: 41 | # 42 | # (2) This script targets any POSIX shell, so it avoids extensions provided 43 | # by Bash, Ksh, etc; in particular arrays are avoided. 44 | # 45 | # The "traditional" practice of packing multiple parameters into a 46 | # space-separated string is a well documented source of bugs and security 47 | # problems, so this is (mostly) avoided, by progressively accumulating 48 | # options in "$@", and eventually passing that to Java. 49 | # 50 | # Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, 51 | # and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; 52 | # see the in-line comments for details. 53 | # 54 | # There are tweaks for specific operating systems such as AIX, CygWin, 55 | # Darwin, MinGW, and NonStop. 56 | # 57 | # (3) This script is generated from the Groovy template 58 | # https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt 59 | # within the Gradle project. 60 | # 61 | # You can find Gradle at https://github.com/gradle/gradle/. 62 | # 63 | ############################################################################## 64 | 65 | # Attempt to set APP_HOME 66 | 67 | # Resolve links: $0 may be a link 68 | app_path=$0 69 | 70 | # Need this for daisy-chained symlinks. 71 | while 72 | APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path 73 | [ -h "$app_path" ] 74 | do 75 | ls=$( ls -ld "$app_path" ) 76 | link=${ls#*' -> '} 77 | case $link in #( 78 | /*) app_path=$link ;; #( 79 | *) app_path=$APP_HOME$link ;; 80 | esac 81 | done 82 | 83 | APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit 84 | 85 | APP_NAME="Gradle" 86 | APP_BASE_NAME=${0##*/} 87 | 88 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 89 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' 90 | 91 | # Use the maximum available, or set MAX_FD != -1 to use that value. 92 | MAX_FD=maximum 93 | 94 | warn () { 95 | echo "$*" 96 | } >&2 97 | 98 | die () { 99 | echo 100 | echo "$*" 101 | echo 102 | exit 1 103 | } >&2 104 | 105 | # OS specific support (must be 'true' or 'false'). 106 | cygwin=false 107 | msys=false 108 | darwin=false 109 | nonstop=false 110 | case "$( uname )" in #( 111 | CYGWIN* ) cygwin=true ;; #( 112 | Darwin* ) darwin=true ;; #( 113 | MSYS* | MINGW* ) msys=true ;; #( 114 | NONSTOP* ) nonstop=true ;; 115 | esac 116 | 117 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 118 | 119 | 120 | # Determine the Java command to use to start the JVM. 121 | if [ -n "$JAVA_HOME" ] ; then 122 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 123 | # IBM's JDK on AIX uses strange locations for the executables 124 | JAVACMD=$JAVA_HOME/jre/sh/java 125 | else 126 | JAVACMD=$JAVA_HOME/bin/java 127 | fi 128 | if [ ! -x "$JAVACMD" ] ; then 129 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 130 | 131 | Please set the JAVA_HOME variable in your environment to match the 132 | location of your Java installation." 133 | fi 134 | else 135 | JAVACMD=java 136 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 137 | 138 | Please set the JAVA_HOME variable in your environment to match the 139 | location of your Java installation." 140 | fi 141 | 142 | # Increase the maximum file descriptors if we can. 143 | if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then 144 | case $MAX_FD in #( 145 | max*) 146 | MAX_FD=$( ulimit -H -n ) || 147 | warn "Could not query maximum file descriptor limit" 148 | esac 149 | case $MAX_FD in #( 150 | '' | soft) :;; #( 151 | *) 152 | ulimit -n "$MAX_FD" || 153 | warn "Could not set maximum file descriptor limit to $MAX_FD" 154 | esac 155 | fi 156 | 157 | # Collect all arguments for the java command, stacking in reverse order: 158 | # * args from the command line 159 | # * the main class name 160 | # * -classpath 161 | # * -D...appname settings 162 | # * --module-path (only if needed) 163 | # * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. 164 | 165 | # For Cygwin or MSYS, switch paths to Windows format before running java 166 | if "$cygwin" || "$msys" ; then 167 | APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) 168 | CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) 169 | 170 | JAVACMD=$( cygpath --unix "$JAVACMD" ) 171 | 172 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 173 | for arg do 174 | if 175 | case $arg in #( 176 | -*) false ;; # don't mess with options #( 177 | /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath 178 | [ -e "$t" ] ;; #( 179 | *) false ;; 180 | esac 181 | then 182 | arg=$( cygpath --path --ignore --mixed "$arg" ) 183 | fi 184 | # Roll the args list around exactly as many times as the number of 185 | # args, so each arg winds up back in the position where it started, but 186 | # possibly modified. 187 | # 188 | # NB: a `for` loop captures its iteration list before it begins, so 189 | # changing the positional parameters here affects neither the number of 190 | # iterations, nor the values presented in `arg`. 191 | shift # remove old arg 192 | set -- "$@" "$arg" # push replacement arg 193 | done 194 | fi 195 | 196 | # Collect all arguments for the java command; 197 | # * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of 198 | # shell script including quotes and variable substitutions, so put them in 199 | # double quotes to make sure that they get re-expanded; and 200 | # * put everything else in single quotes, so that it's not re-expanded. 201 | 202 | set -- \ 203 | "-Dorg.gradle.appname=$APP_BASE_NAME" \ 204 | -classpath "$CLASSPATH" \ 205 | org.gradle.wrapper.GradleWrapperMain \ 206 | "$@" 207 | 208 | # Stop when "xargs" is not available. 209 | if ! command -v xargs >/dev/null 2>&1 210 | then 211 | die "xargs is not available" 212 | fi 213 | 214 | # Use "xargs" to parse quoted args. 215 | # 216 | # With -n1 it outputs one arg per line, with the quotes and backslashes removed. 217 | # 218 | # In Bash we could simply go: 219 | # 220 | # readarray ARGS < <( xargs -n1 <<<"$var" ) && 221 | # set -- "${ARGS[@]}" "$@" 222 | # 223 | # but POSIX shell has neither arrays nor command substitution, so instead we 224 | # post-process each arg (as a line of input to sed) to backslash-escape any 225 | # character that might be a shell metacharacter, then use eval to reverse 226 | # that process (while maintaining the separation between arguments), and wrap 227 | # the whole thing up as a single "set" statement. 228 | # 229 | # This will of course break if any of these variables contains a newline or 230 | # an unmatched quote. 231 | # 232 | 233 | eval "set -- $( 234 | printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | 235 | xargs -n1 | 236 | sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | 237 | tr '\n' ' ' 238 | )" '"$@"' 239 | 240 | exec "$JAVACMD" "$@" 241 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%"=="" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%"=="" set DIRNAME=. 29 | set APP_BASE_NAME=%~n0 30 | set APP_HOME=%DIRNAME% 31 | 32 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 33 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 34 | 35 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 36 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 37 | 38 | @rem Find java.exe 39 | if defined JAVA_HOME goto findJavaFromJavaHome 40 | 41 | set JAVA_EXE=java.exe 42 | %JAVA_EXE% -version >NUL 2>&1 43 | if %ERRORLEVEL% equ 0 goto execute 44 | 45 | echo. 46 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 47 | echo. 48 | echo Please set the JAVA_HOME variable in your environment to match the 49 | echo location of your Java installation. 50 | 51 | goto fail 52 | 53 | :findJavaFromJavaHome 54 | set JAVA_HOME=%JAVA_HOME:"=% 55 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 56 | 57 | if exist "%JAVA_EXE%" goto execute 58 | 59 | echo. 60 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 61 | echo. 62 | echo Please set the JAVA_HOME variable in your environment to match the 63 | echo location of your Java installation. 64 | 65 | goto fail 66 | 67 | :execute 68 | @rem Setup the command line 69 | 70 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 71 | 72 | 73 | @rem Execute Gradle 74 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* 75 | 76 | :end 77 | @rem End local scope for the variables with windows NT shell 78 | if %ERRORLEVEL% equ 0 goto mainEnd 79 | 80 | :fail 81 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 82 | rem the _cmd.exe /c_ return code! 83 | set EXIT_CODE=%ERRORLEVEL% 84 | if %EXIT_CODE% equ 0 set EXIT_CODE=1 85 | if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% 86 | exit /b %EXIT_CODE% 87 | 88 | :mainEnd 89 | if "%OS%"=="Windows_NT" endlocal 90 | 91 | :omega 92 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | **Tutorial ContactBook** 2 | 3 | A project to teach how to provision an aws infraestructure and deploy an SpringBoot 4 | Application in an Amazon EC2 machine using Terraform and Docker. 5 | 6 | **Level 1** 7 | 8 | 9 | This level will teach you how to : 10 | - Run a Docker container with Postgres using a docker-compose file 11 | - Connect your springboot app with Postgres database container. 12 | 13 | **Level 2** 14 | 15 | 16 | This level will teach you how to : 17 | - run a Docker build your application image 18 | - configure your image to docker-compose file 19 | - run your image on a docker container 20 | - Access your application using swagger and postman 21 | - Upload your api image to dockerhub 22 | 23 | **Level 3** 24 | 25 | 26 | This level will teach you: 27 | - Understanding AWS basic infraestructure 28 | - Create IAM user 29 | - Configure aws cli profile 30 | - Create terraform files needed 31 | - Create ssh key pair 32 | - Configure userdata template file 33 | - Terraform commands 34 | - Access ec2 host from local machine 35 | - Access your api running on ec2 using swagger and postman -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'tutorial-spring-docker-terraform-aws' 2 | -------------------------------------------------------------------------------- /src/main/java/com/maraujo/contactbook/ProjetoModeloApplication.java: -------------------------------------------------------------------------------- 1 | package com.maraujo.contactbook; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.openfeign.EnableFeignClients; 6 | import org.springframework.data.jpa.repository.config.EnableJpaRepositories; 7 | 8 | @EnableJpaRepositories 9 | @EnableFeignClients 10 | @SpringBootApplication 11 | public class ProjetoModeloApplication { 12 | 13 | public static void main(String[] args) { 14 | SpringApplication.run(ProjetoModeloApplication.class, args); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/maraujo/contactbook/controller/ContactbookController.java: -------------------------------------------------------------------------------- 1 | package com.maraujo.contactbook.controller; 2 | 3 | import com.maraujo.contactbook.controller.request.ContactRequest; 4 | import com.maraujo.contactbook.service.interfaces.ContactbookService; 5 | import lombok.extern.log4j.Log4j2; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.http.ResponseEntity; 8 | import org.springframework.web.bind.annotation.*; 9 | 10 | @Log4j2 11 | @RestController 12 | @RequestMapping("/contact") 13 | public class ContactbookController { 14 | 15 | @Autowired 16 | private ContactbookService contactbookService; 17 | 18 | @PostMapping 19 | public ResponseEntity save(@RequestBody ContactRequest request){ 20 | log.info("M save, request={}", request); 21 | return ResponseEntity.ok(contactbookService.save(request.toEntity())); 22 | } 23 | 24 | @GetMapping(path = "/{name}") 25 | public ResponseEntity getByName(@PathVariable("name") String name){ 26 | log.info("M getByName, name={}", name); 27 | return ResponseEntity.ok(contactbookService.getByName(name)); 28 | } 29 | 30 | @GetMapping 31 | public ResponseEntity getAll(){ 32 | log.info("M getAll, NEW"); 33 | return ResponseEntity.ok(contactbookService.getAll()); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/com/maraujo/contactbook/controller/handler/ControllerAdvice.java: -------------------------------------------------------------------------------- 1 | package com.maraujo.contactbook.controller.handler; 2 | 3 | import org.springframework.web.bind.annotation.ExceptionHandler; 4 | import org.springframework.web.bind.annotation.RestControllerAdvice; 5 | 6 | @RestControllerAdvice 7 | public class ControllerAdvice { 8 | 9 | @ExceptionHandler(value = {Exception.class}) 10 | public String handler(Exception ex){ 11 | return ex.getMessage(); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/maraujo/contactbook/controller/request/ContactRequest.java: -------------------------------------------------------------------------------- 1 | package com.maraujo.contactbook.controller.request; 2 | 3 | import com.maraujo.contactbook.entity.Contact; 4 | import lombok.*; 5 | 6 | @Getter 7 | @Setter 8 | @AllArgsConstructor 9 | @RequiredArgsConstructor 10 | @Builder 11 | public class ContactRequest { 12 | private String name; 13 | private String phone; 14 | private String cep; 15 | private Integer numero; 16 | 17 | public Contact toEntity(){ 18 | return Contact.builder() 19 | .name(name) 20 | .phone(phone) 21 | .cep(cep.contains("-")? cep.replace("-", ""): cep) 22 | .numero(numero) 23 | .build(); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/maraujo/contactbook/entity/Contact.java: -------------------------------------------------------------------------------- 1 | package com.maraujo.contactbook.entity; 2 | 3 | import com.maraujo.contactbook.service.client.response.AddressResponse; 4 | import lombok.*; 5 | import org.hibernate.Hibernate; 6 | 7 | import javax.persistence.*; 8 | import java.util.Objects; 9 | 10 | @Entity 11 | @Table(name = "contact_book_tb") 12 | @Builder 13 | @Getter 14 | @Setter 15 | @ToString 16 | @AllArgsConstructor 17 | @NoArgsConstructor 18 | public class Contact { 19 | @Id 20 | @GeneratedValue(strategy = GenerationType.AUTO) 21 | private Long id; 22 | private String name; 23 | private String phone; 24 | private String cep; 25 | private String logradouro; 26 | private Integer numero; 27 | private String bairro; 28 | private String cidade; 29 | private String estado; 30 | 31 | public void setAddress(AddressResponse address){ 32 | this.logradouro = address.getLogradouro(); 33 | this.bairro = address.getBairro(); 34 | this.cidade = address.getCidade(); 35 | this.estado = address.getEstado(); 36 | } 37 | 38 | @Override 39 | public boolean equals(Object o) { 40 | if (this == o) return true; 41 | if (o == null || Hibernate.getClass(this) != Hibernate.getClass(o)) return false; 42 | Contact contact = (Contact) o; 43 | return id != null && Objects.equals(id, contact.id); 44 | } 45 | 46 | @Override 47 | public int hashCode() { 48 | return getClass().hashCode(); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/com/maraujo/contactbook/repository/ContactbookRepository.java: -------------------------------------------------------------------------------- 1 | package com.maraujo.contactbook.repository; 2 | 3 | import com.maraujo.contactbook.entity.Contact; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | import org.springframework.stereotype.Repository; 6 | 7 | import java.util.Optional; 8 | 9 | @Repository 10 | public interface ContactbookRepository extends JpaRepository { 11 | Optional findByName(String name); 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/maraujo/contactbook/service/ContactbookServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.maraujo.contactbook.service; 2 | 3 | import com.maraujo.contactbook.entity.Contact; 4 | import com.maraujo.contactbook.repository.ContactbookRepository; 5 | import com.maraujo.contactbook.service.client.ViaCepClient; 6 | import com.maraujo.contactbook.service.client.response.AddressResponse; 7 | import com.maraujo.contactbook.service.interfaces.ContactbookService; 8 | import lombok.AllArgsConstructor; 9 | import lombok.extern.log4j.Log4j2; 10 | import org.springframework.stereotype.Service; 11 | 12 | import java.util.List; 13 | import java.util.Optional; 14 | 15 | @Log4j2 16 | @Service 17 | @AllArgsConstructor 18 | public class ContactbookServiceImpl implements ContactbookService { 19 | 20 | private final ContactbookRepository repository; 21 | private final ViaCepClient viaCepClient; 22 | 23 | @Override 24 | public Contact save(Contact contact) { 25 | log.info("M save, contact={}, NEW", contact); 26 | try { 27 | AddressResponse address = viaCepClient.getAddress(contact.getCep()); 28 | log.info("M getAddress, address={}", address); 29 | contact.setAddress(address); 30 | log.info("M save, contact={}, SAVING", contact); 31 | return repository.save(contact); 32 | }catch (Exception ex){ 33 | log.info("Cannot find the address with cep={}, ERROR={}.", contact.getCep(), ex.getMessage()); 34 | 35 | throw new IllegalArgumentException("Cannot find the address with cep: " + contact.getCep()); 36 | } 37 | } 38 | 39 | @Override 40 | public List getAll() { 41 | return repository.findAll(); 42 | } 43 | 44 | @Override 45 | public Optional getByName(String name) { 46 | log.info("M getByName, name={}, NEW", name); 47 | return repository.findByName(name); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/com/maraujo/contactbook/service/client/ViaCepClient.java: -------------------------------------------------------------------------------- 1 | package com.maraujo.contactbook.service.client; 2 | 3 | import com.maraujo.contactbook.service.client.response.AddressResponse; 4 | import org.springframework.cloud.openfeign.FeignClient; 5 | import org.springframework.web.bind.annotation.GetMapping; 6 | import org.springframework.web.bind.annotation.PathVariable; 7 | 8 | @FeignClient(name= "viaCep", url = "${viacep.endpoint}") 9 | public interface ViaCepClient { 10 | 11 | @GetMapping(value = "/{cep}/json") 12 | AddressResponse getAddress(@PathVariable(value = "cep") String cep); 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/maraujo/contactbook/service/client/response/AddressResponse.java: -------------------------------------------------------------------------------- 1 | package com.maraujo.contactbook.service.client.response; 2 | 3 | import com.fasterxml.jackson.annotation.JsonIgnoreProperties; 4 | import com.fasterxml.jackson.annotation.JsonProperty; 5 | import lombok.*; 6 | 7 | @Getter 8 | @Setter 9 | @ToString 10 | @RequiredArgsConstructor 11 | @JsonIgnoreProperties(ignoreUnknown = true) 12 | public class AddressResponse { 13 | 14 | private String cep; 15 | private String logradouro; 16 | private String bairro; 17 | @JsonProperty(value = "localidade") 18 | private String cidade; 19 | @JsonProperty(value = "uf") 20 | private String estado; 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/maraujo/contactbook/service/interfaces/ContactbookService.java: -------------------------------------------------------------------------------- 1 | package com.maraujo.contactbook.service.interfaces; 2 | 3 | import com.maraujo.contactbook.entity.Contact; 4 | 5 | import java.util.List; 6 | import java.util.Optional; 7 | 8 | public interface ContactbookService { 9 | Contact save(Contact contact); 10 | List getAll(); 11 | Optional getByName(String name); 12 | } 13 | -------------------------------------------------------------------------------- /src/main/resources/application-prod.yml: -------------------------------------------------------------------------------- 1 | #arquivo de configuracoes local/default 2 | 3 | #port config 4 | server: 5 | port: ${SERVER_PORT} 6 | 7 | #profile config 8 | spring: 9 | application: 10 | name: "contactbook" 11 | 12 | config: 13 | activate: 14 | on-profile: 15 | - prod 16 | 17 | #database config 18 | datasource: 19 | url: jdbc:postgresql://postgres-db:5432/contactbook?currentSchema=public 20 | username: "admin" 21 | password: "admin" 22 | driver-class-name: "org.postgresql.Driver" 23 | 24 | jpa: 25 | properties: 26 | hibernate: 27 | dialect: org.hibernate.dialect.PostgreSQLDialect 28 | 29 | #flyway config 30 | flyway: 31 | locations: "db/migration" 32 | baseline-on-migrate: false 33 | 34 | #devtools config 35 | devtools: 36 | restart: 37 | enabled: true 38 | 39 | #swagger config 40 | springdoc: 41 | swagger-ui: 42 | path: "/contactbook/swagger-ui.html" 43 | 44 | #viacep ulr endpoint 45 | viacep: 46 | endpoint: "viacep.com.br/ws" -------------------------------------------------------------------------------- /src/main/resources/application-qa.yml: -------------------------------------------------------------------------------- 1 | #arquivo de configuracoes local/default 2 | 3 | #port config 4 | server: 5 | port: ${SERVER_PORT} 6 | 7 | #profile config 8 | spring: 9 | application: 10 | name: "contactbook" 11 | 12 | config: 13 | activate: 14 | on-profile: 15 | - qa 16 | 17 | #database config 18 | datasource: 19 | url: jdbc:postgresql://postgres-db:5432/contactbook?currentSchema=public 20 | username: "admin" 21 | password: "admin" 22 | driver-class-name: "org.postgresql.Driver" 23 | 24 | jpa: 25 | properties: 26 | hibernate: 27 | dialect: org.hibernate.dialect.PostgreSQLDialect 28 | 29 | #flyway config 30 | flyway: 31 | locations: "db/migration" 32 | baseline-on-migrate: false 33 | 34 | #devtools config 35 | devtools: 36 | restart: 37 | enabled: true 38 | 39 | #swagger config 40 | springdoc: 41 | swagger-ui: 42 | path: "/contactbook/swagger-ui.html" 43 | 44 | #viacep ulr endpoint 45 | viacep: 46 | endpoint: "viacep.com.br/ws" -------------------------------------------------------------------------------- /src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | #arquivo de configuracoes local/default 2 | 3 | #port config 4 | server: 5 | port: 8090 6 | 7 | spring: 8 | application: 9 | name: "contactbook" 10 | 11 | #database config 12 | datasource: 13 | url: jdbc:postgresql://localhost:15432/contactbook?currentSchema=public 14 | username: "admin" 15 | password: "admin" 16 | driver-class-name: "org.postgresql.Driver" 17 | hikari: 18 | schema: public 19 | 20 | jpa: 21 | hibernate: 22 | ddl-auto: update 23 | properties: 24 | hibernate: 25 | dialect: org.hibernate.dialect.PostgreSQLDialect 26 | 27 | #flyway config 28 | flyway: 29 | locations: "db/migration" 30 | baseline-on-migrate: false 31 | 32 | #devtools config 33 | devtools: 34 | restart: 35 | enabled: true 36 | 37 | #swagger config 38 | springdoc: 39 | swagger-ui: 40 | path: "/contactbook/swagger-ui.html" 41 | 42 | #viacep url endpoint 43 | viacep: 44 | endpoint: "viacep.com.br/ws" 45 | 46 | 47 | -------------------------------------------------------------------------------- /src/main/resources/db/migration/V1_1__create_table_contactbook.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE if not exists contact_book_tb ( 2 | id BIGINT NOT NULL, 3 | name VARCHAR(255), 4 | phone VARCHAR(255), 5 | cep VARCHAR(255), 6 | logradouro VARCHAR(255), 7 | numero INTEGER, 8 | complemento VARCHAR(255), 9 | bairro VARCHAR(255), 10 | cidade VARCHAR(255), 11 | estado VARCHAR(255), 12 | CONSTRAINT pk_contact_book_tb PRIMARY KEY (id) 13 | ); -------------------------------------------------------------------------------- /src/test/java/com/maraujo/contactbook/ProjetoModeloApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.maraujo.contactbook; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class ProjetoModeloApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | --------------------------------------------------------------------------------