terraform {
required_version = ">= 1.0"
}
provider "aws" {
region = var.aws_region
}
resource "aws_s3_bucket" "cicdddterraform" {
bucket = "cicdddterraform"
tags = {
"Name" = "cicdddterraform"
}
}
resource "aws_dynamodb_table" "cicdddterraform-tf-lock" {
depends_on = [aws_s3_bucket.cicdddterraform]
name = "cicdddterraform-tf-lock"
billing_mode = "PAY_PER_REQUEST"
hash_key = "LockID"
attribute {
name = "LockID"
type = "S"
}
tags = {
"Name" = "cicdddterraform-tf-lock"
}
}
variable "aws_region" {
default = "ap-northeast-2"
}
pipeline {
agent {
label 'Slave01'
}
parameters {
string(name: 'environment', defaultValue: 'terraform', description: 'Workspace/environment file to use for deployment')
booleanParam(name: 'autoApprove', defaultValue: false, description: 'Automatically run apply after generating plan?')
}
environment {
AWS_ACCESS_KEY_ID = credentials('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = credentials('AWS_SECRET_ACCESS_KEY')
REGION = credentials('AWS_REGION')
}
stages {
stage('Terraform - Backend') {
when {
expression { !params.performDestroy }
}
steps {
dir('terraform-backend') {
script {
sh 'terraform init -upgrade'
sh "terraform validate"
sh "terraform plan"
}
}
}
}
stage('Terraform - Backend Apply') {
when {
expression { !params.performDestroy }
}
steps {
dir('terraform-backend') {
script {
sh "terraform apply --auto-approve"
}
}
}
}
}
}
wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt update && sudo apt install terraform
export PATH=$PATH:/path/to/terraform/directory
terraform --version