add tf1.0 example

This commit is contained in:
jem 2021-10-15 14:08:12 +02:00
parent 894a8764ee
commit 1ad655df87
4 changed files with 87 additions and 11 deletions

View file

@ -0,0 +1,4 @@
bucket = "my-configuration"
key = "my-module"
kms_key_id = "arn:aws:kms:my-kms-alias"
region = "my-region"

View file

@ -0,0 +1,55 @@
from os import environ
from pybuilder.core import task, init
from ddadevops import *
name = 'my-project'
MODULE = 'my-module'
PROJECT_ROOT_PATH = '../../..'
class MyBuild(AwsBackendPropertiesMixin, DevopsTerraformBuild):
pass
@init
def initialize(project):
project.build_depends_on('ddadevops>=0.13.2')
stage = environ['STAGE']
print('Doing stage: ', stage)
config = create_devops_terraform_build_config(stage,
PROJECT_ROOT_PATH,
MODULE,
{},
use_workspace=False,
terraform_version=1.0)
config = add_aws_backend_properties_mixin_config(config, 'prod')
build = MyBuild(project, config)
build.initialize_build_dir()
@task
def plan(project):
build = get_devops_build(project)
build.plan()
@task
def tf_apply(project):
build = get_devops_build(project)
build.apply(True)
@task
def apply(project):
build = get_devops_build(project)
build.apply(True)
@task
def destroy(project):
build = get_devops_build(project)
build.destroy(True)
@task
def tf_import(project):
build = get_devops_build(project)
build.tf_import('aws_route53_record.v4', 'my-resource-id')

View file

@ -0,0 +1,7 @@
resource "aws_route53_record" "v4" {
zone_id = "my-zone-id-${var.stage}"
name = "test"
type = "A"
ttl = "300"
records = ["8.8.8.8"]
}

View file

@ -20,7 +20,7 @@ def create_devops_terraform_build_config(stage,
terraform_build_commons_dir_name='terraform',
debug_print_terraform_command=False,
additional_tfvar_files=[],
terraform_version=1.0):
terraform_version=0.15):
ret = create_devops_build_config(
stage, project_root_path, module, build_dir_name)
ret.update({'additional_vars': additional_vars,
@ -30,7 +30,7 @@ def create_devops_terraform_build_config(stage,
'build_commons_path': build_commons_path,
'terraform_build_commons_dir_name': terraform_build_commons_dir_name,
'debug_print_terraform_command': debug_print_terraform_command,
'additional_tfvar_files': additional_tfvar_files
'additional_tfvar_files': additional_tfvar_files,
'terraform_version': terraform_version})
return ret
@ -144,6 +144,16 @@ class DevopsTerraformBuild(DevopsBuild):
def apply(self, auto_approve=False):
tf = self.init_client()
if auto_approve:
auto_approve_flag = IsFlagged
else:
auto_approve_flag = None
if self.terraform_version >= 1.0:
return_code, stdout, stderr = tf.apply(capture_output=False, raise_on_error=True,
auto_approve=auto_approve_flag,
var=self.project_vars(),
var_file=self.additional_tfvar_files)
else:
return_code, stdout, stderr = tf.apply(capture_output=False, raise_on_error=True,
skip_plan=auto_approve,
var=self.project_vars(),
@ -168,17 +178,17 @@ class DevopsTerraformBuild(DevopsBuild):
def destroy(self, auto_approve=False):
tf = self.init_client()
if auto_approve:
force = IsFlagged
auto_approve_flag = IsFlagged
else:
force = None
if self.terraform_version <= 1.0:
auto_approve_flag = None
if self.terraform_version >= 1.0:
return_code, stdout, stderr = tf.destroy(capture_output=False, raise_on_error=True,
auto_approve=force,
auto_approve=auto_approve_flag,
var=self.project_vars(),
var_file=self.additional_tfvar_files)
else:
return_code, stdout, stderr = tf.destroy(capture_output=False, raise_on_error=True,
force=force,
force=auto_approve_flag,
var=self.project_vars(),
var_file=self.additional_tfvar_files)
self.post_build()