automate-iac
folder and open it in your prefered IDE. Let's create a python environment and install dependencies. For this example we are using cf2tf module to convert cloudformation to terraform.python -m venv env
env/bin/pip install boto3 cf2tf
create_generated_template
call. Refer to the docs for configuration of various resources. Once the template is created you can retrieve the YAML and save it locally with the get_generated_template
call. And just like that you have a cloudformation template that you can deploy in another account, etc# create cloudformation client
client = boto3.client('cloudformation')
# generate template
response = client.create_generated_template(
Resources=[
{
'ResourceType': 'AWS::DynamoDB::Table',
'LogicalResourceId': 'mytable',
'ResourceIdentifier': {
'TableName': 'mytablename'
}
},
],
GeneratedTemplateName='mytablename_template',
TemplateConfiguration={
'DeletionPolicy': 'RETAIN',
'UpdateReplacePolicy': 'RETAIN'
}
)
# get template body
response = client.get_generated_template(
Format='YAML',
GeneratedTemplateName='mytablename_template'
).get('TemplateBody')
# save template locally to a file
with open('cf.yaml','w') as f:
f.writelines(response)
cf2tf
comes in handy. Specify the source and destination locations and let TemplateConverter
do the magic!# specify the path to the cloudformation file
tmpl_path = Path('cf.yaml')
# specify to write the resultant tf file to the current working directory
output_writer = cf2tf.save.create_writer('./')
cf_template = Template.from_yaml(tmpl_path).template
# convert cloudformation template into a terraform configuration
search_manager = code.search_manager()
config = TemplateConverter(tmpl_path.stem, cf_template, search_manager).convert()
# Save configuration to disk
config.save(output_writer)
import boto3
import cf2tf.save
from cf2tf.cloudformation import Template
from cf2tf.convert import TemplateConverter
from cf2tf.terraform import code
from pathlib import Path
# create cloudformation client
client = boto3.client('cloudformation')
# generate template
response = client.create_generated_template(
Resources=[
{
'ResourceType': 'AWS::DynamoDB::Table',
'LogicalResourceId': 'mytable',
'ResourceIdentifier': {
'TableName': 'mytablename'
}
},
],
GeneratedTemplateName='mytablename_template',
TemplateConfiguration={
'DeletionPolicy': 'RETAIN',
'UpdateReplacePolicy': 'RETAIN'
}
)
# get template body
response = client.get_generated_template(
Format='YAML',
GeneratedTemplateName='mytablename_template'
).get('TemplateBody')
# save template locally to a file
with open('cf.yaml','w') as f:
f.writelines(response)
# specify the path to the cloudformation file
tmpl_path = Path('cf.yaml')
# specify to write the resultant tf file to the current working directory
output_writer = cf2tf.save.create_writer('./')
cf_template = Template.from_yaml(tmpl_path).template
# convert cloudformation template into a terraform configuration
search_manager = code.search_manager()
config = TemplateConverter(tmpl_path.stem, cf_template, search_manager).convert()
# Save configuration to disk
config.save(output_writer)