Giter VIP home page Giter VIP logo

cf-to-tf's Introduction

CloudFormation to Terraform
A simple cli tool for generating Terraform configuration for existing CloudFormation templates.
Build status
A Human Made project. Maintained by @nathanielks.

CloudFormation to Terraform

This node CLI tool is used for generating both Terraform configuration files as well as Terraform state so that you can use Terraform to manage CloudFormation templates. To further clarify, it does not generate terraform configuration for the individual resources CloudFormation provisions, it generates an aws_cloudformation_stack resource so that you can manage your existing CloudFormation stacks with Terraform instead of or in conjunction with the AWS Console and CLI.

Getting Started

npm i -g @humanmade/cf-to-tf

Recommended Dependencies

As this was designed to generate Terraform resources, it'd be a good idea to install terraform. You can install the binary by itself or use a tool like brew to manage it for you.

It's also recommended to install json2hcl as this will assist in processing output from cf-to-tf later.

Demo

Let's use the following CloudFormation Stack response as an example:

{
    "Stacks": [
        {
            "StackId": "arn:aws:cloudformation:eu-central-1:123456789012:stack/foobarbaz/255491f0-71b8-11e7-a154-500c52a6cefe",
            "Description": "FooBar Stack",
            "Parameters": [
                {
                    "ParameterValue": "bar",
                    "ParameterKey": "foo"
                },
                {
                    "ParameterValue": "baz",
                    "ParameterKey": "bar"
                },
                {
                    "ParameterValue": "qux",
                    "ParameterKey": "baz"

                }
            ],
            "Tags": [
                {
                    "Value": "bar",
                    "Key": "foo"
                },
                {
                    "Value": "qux",
                    "Key": "baz"
                }
            ],
            "Outputs": [
                {
                    "Description": "Foobarbaz",
                    "OutputKey": "FooBarBaz",
                    "OutputValue": "output value"
                }
            ],
            "CreationTime": "2017-07-26T04:08:57.266Z",
            "Capabilities": [
                "CAPABILITY_IAM"
            ],
            "StackName": "foobarbaz",
            "NotificationARNs": [],
            "StackStatus": "CREATE_COMPLETE",
            "DisableRollback": true
        }
    ]
}

Running cf-to-tf --stack foobarbaz config | json2hcl | cf-to-tf clean-hcl | terraform fmt - will generate the following config:

resource "aws_cloudformation_stack" "network" {
  capabilities     = ["CAPABILITY_IAM"]
  disable_rollback = true
  name             = "foobarbaz"

  parameters = {
    foo = "bar"
    bar = "baz"
    baz = "qux"
  }

  tags = {
    foo = "bar"
    baz = "qux"
  }
}

Usage

Usage: cf-to-tf [options] [command]


Options:

  -s, --stack <stack>                 The CloudFormation stack to import
  -r, --resource-name <resourceName>  The name to assign the terraform resource
  -h, --help                          output usage information


Commands:

  config      Generates Terraform configuration in JSON
  state       Generates Terraform state file in JSON
  template    Prints the CloudFormation Stack Template
  clean-hcl   Cleans generated HCL according to my preferences

Terraform JSON Files

This tool is designed to be used in conjunction with other tools. It will only output the data to STDOUT and is designed to be piped to another program to write the file to a location. For example, to generate a configuration file for a stack named lambda-resources, we could do the following:

cf-to-tf -s lambda-resources config | tee main.tf.json

This command will fetch a CloudFormation stack named lambda-resources and generate the required Terraform configuration for it. We then pipe the output to tee which will write to a file named main.tf.json. Because HCL is JSON compatible, Terraform can read the main.tf.json natively.

Terraform State

To generate the associated Terraform state for this CloudFormation stack, you would run the following:

cf-to-tf -s lambda-resources state | tee terraform.tfstate

This will create a state file from scratch. It assumes you don't already have an existing state file in place. I'm considering updating the tool to write just the resource portion of the state so it can be added to an existing state file, but that wasn't an immediate priority.

Pretty Printing

Both of these commands will generate compressed JSON output, meaning whitespace has been stripped. To pretty print the output for enhanced readability, you could pipe the output to jq, and then to tee:

cf-to-tf -s lambda-resources config | jq '.' | tee main.tf.json
cf-to-tf -s lambda-resources state | jq '.' | tee terraform.tfstate

Generating HCL

It's also possible to use a tool called json2hcl to generate HCL:

cf-to-tf -s lambda-resources config | json2hcl | tee main.tf

Unfortunately, while json2hcl outputs valid HCL, it's not in the format I like. To solve that problem, the clean-hcl command is also available. To output HCL in the format you'll normally see, you can execute this chain:

cf-to-tf -s lambda-resources config | json2hcl | cf-to-tf clean-hcl | terraform fmt - | tee main.tf

We're doing the same thing we were doing before, but now we're also piping the result to cf-to-tf clean-hcl which formats the file a certain way, then piping it to terraform fmt - which formats the file further (primarily, this tool aligns = and adds newlines where necessary).

Reading from STDIN

It's also possible to have cf-to-tf read stack data from STDIN. For example, if you have the JSON response from the aws-cli call stored in a variable for re-use, you can do the following:

JSON="$(aws cloudformation describe-stacks --stack-name lambda-resources)"
echo "$JSON" | cf-to-tf -s - config

AWS Authentication

The command uses the AWS SDK under the hood to retrieve the CloudFormation stack details, so set your authentication credentials as you would normally (~/.aws/credentials, AWS_PROFILE, AWS_REGION, etc).

Batch Import scripts

For an example of how to use this script in batch operations importing multiple stacks in multiple regions, refer to this gist.

cf-to-tf's People

Contributors

nathanielks avatar zackse avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

cf-to-tf's Issues

node --harmony

$ cf-to-tf -h
module.js:549
throw err;

Error: Cannot find module '/home/juan/node --harmony'
at Function.Module._resolveFilename (module.js:547:15)
at Function.Module._load (module.js:474:25)
at Function.Module.runMain (module.js:693:10)
at startup (bootstrap_node.js:188:16)
at bootstrap_node.js:609:3

Please help, thanks.

Not able to generate tfstate file

I am running this command cf-to-tf -s RDS-STACK-eks-adhng config state | tee terraform.tfstate but getting below error
and also how it will get this stack details .

/usr/lib/node_modules/@humanmade/cf-to-tf/index.js:13
stack: opts.parent.stack,
^

TypeError: Cannot read property 'stack' of undefined
at formatOptions (/usr/lib/node_modules/@humanmade/cf-to-tf/index.js:13:21)
at Command.program.command.description.action.options (/usr/lib/node_modules/@humanmade/cf-to-tf/index.js:44:16)

Cloudformation template support?

Hey, great tool! Works perfectly for a cloudformation stack that has already been deployed! Any plans on adding support for converting cloudformation templates to tf format?

AWS has some very nice CF templates for production-ready clusters for quite a few apps: https://aws.amazon.com/quickstart/

The one for nomad, for example, sets up VPC, a bastion, consul, etc: https://aws.amazon.com/quickstart/architecture/nomad/

Currently learning terraform (and the rest of the hashicorp stack), I'd like to avoid having to add CF to that list.

Thanks!

Installation failed

Hi,

I want to convert my cloudformation templates into readable terraform configuration.

Unfortunately, I can't even install cf-to-tf (I'm on a debian stretch).

When trying :

npm i -g cf-to-tf

I have

npm ERR! Linux 4.13.5-200.fc26.x86_64
npm ERR! argv "/usr/bin/node" "/usr/bin/npm" "i" "-g" "cf-to-tf"
npm ERR! node v6.11.4
npm ERR! npm  v3.10.10

npm ERR! Cannot convert undefined or null to object
npm ERR! 
npm ERR! If you need help, you may report this error at:
npm ERR!     <https://github.com/npm/npm/issues>

npm ERR! Please include the following file with any support request:
npm ERR!     /npm-debug.log

Regards,

\'stackName\' failed to satisfy constraint

$ cf-to-tf -s https://aws-quickstart.s3.amazonaws.com/quickstart-atlassian-jira/templates/JiraDataCenter.template config
{ ValidationError: 1 validation error detected: Value 'https://aws-quickstart.s3.amazonaws.com/quickstart-atlassian-jira/templates/JiraDataCenter.template' at 'stackName' failed to satisfy constraint: Member must satisfy regular expression pattern: [a-zA-Z][-a-zA-Z0-9]*|arn:[-a-zA-Z0-9:/._+]*
    at Request.extractError (/usr/local/lib/node_modules/@humanmade/cf-to-tf/node_modules/aws-sdk/lib/protocol/query.js:47:29)
    at Request.callListeners (/usr/local/lib/node_modules/@humanmade/cf-to-tf/node_modules/aws-sdk/lib/sequential_executor.js:109:20)
    at Request.emit (/usr/local/lib/node_modules/@humanmade/cf-to-tf/node_modules/aws-sdk/lib/sequential_executor.js:81:10)
    at Request.emit (/usr/local/lib/node_modules/@humanmade/cf-to-tf/node_modules/aws-sdk/lib/request.js:683:14)
    at Request.transition (/usr/local/lib/node_modules/@humanmade/cf-to-tf/node_modules/aws-sdk/lib/request.js:22:10)
    at AcceptorStateMachine.runTo (/usr/local/lib/node_modules/@humanmade/cf-to-tf/node_modules/aws-sdk/lib/state_machine.js:14:12)
    at /usr/local/lib/node_modules/@humanmade/cf-to-tf/node_modules/aws-sdk/lib/state_machine.js:26:10
    at Request.<anonymous> (/usr/local/lib/node_modules/@humanmade/cf-to-tf/node_modules/aws-sdk/lib/request.js:38:9)
    at Request.<anonymous> (/usr/local/lib/node_modules/@humanmade/cf-to-tf/node_modules/aws-sdk/lib/request.js:685:12)
    at Request.callListeners (/usr/local/lib/node_modules/@humanmade/cf-to-tf/node_modules/aws-sdk/lib/sequential_executor.js:119:18)
  message: '1 validation error detected: Value \'https://aws-quickstart.s3.amazonaws.com/quickstart-atlassian-jira/templates/JiraDataCenter.template\' at \'stackName\' failed to satisfy constraint: Member must satisfy regular expression pattern: [a-zA-Z][-a-zA-Z0-9]*|arn:[-a-zA-Z0-9:/._+]*',
  code: 'ValidationError',
  time: 2018-09-06T12:28:27.013Z,
  requestId: '5ace66d9-b1d0-11e8-b8e6-6f4fd45d5ff4',
  statusCode: 400,
  retryable: false,
  retryDelay: 66.58297130778628 }

template call fails

Hi,

When running the call with the template flag it fails:

cf-to-tf -s sdlf-cicd-team-repos template
SyntaxError: Unexpected token A in JSON at position 0
at JSON.parse ()
at /usr/local/lib/node_modules/@humanmade/cf-to-tf/lib/template.js:9:24
at processTicksAndRejections (internal/process/task_queues.js:93:5)

No longer maintained

Hello everyone!

I was the primary maintainer for this repo and wanted to say it is no longer being maintained. I don't have the time to respond to issues or make any code changes. My apologies!

Unable to parse json error

While running cf-to-tf --stack cstack.yml config | json2hcl | cf-to-tf clean-hcl | terraform fmt, I get this error:
unable to parse JSON: 1:5: illegal char

Installing and Using this script is a hassle

I have tried installing and using this script, and from the readme, it's not so easy, even as the README doesn't carry the latest versions, and running the commands on my mac, throws all sorts of errors.
Even after I visited the releases page, from the latest tar.gz it doesn't install as expected.

Please assist.

Parse Error

Running the command against a real-life CloudFormation Template.
cf-to-tf --stack - config < /home/Username/bastion.cfn | json2hcl | cf-to-tf clean-hcl | terraform fmt - | tee /home/Username/Bastions.tf.json

and get the error
unable to parse JSON: 1:9: illegal char

I shorten the first command to
cf-to-tf --stack - config < /home/Username/bastion.cfn

and get the error
TypeError: Cannot read property '0' of undefined at getStack.then.result (/usr/local/lib/node_modules/@humanmade/cf-to-tf/index.js:47:54) at <anonymous> at process._tickDomainCallback (internal/process/next_tick.js:229:7)

nested cloudformation to terraform

Hi, I have nested cloud formation stack one for RDS, AMI, VPC, etc...
How to use "cf-to-tf" to convert nested CF stacks to terraform.?
Regards,
Mahu

JSON Parsing Error...?

Running the comand cf-to-tf --stack /home/Username/Desktop/Test config | json2hcl | cf-to-tf clean-hcl | terraform fmt -

getting the error Error running fmt: In <standard input>: key 'illegal' expected start of object ('{') or assignment ('=')

Shortening the first command to cf-to-tf --stack /home/Username/Desktop/Test config | json2hcl | cf-to-tf clean-hcl

I get the error unable to parse JSON: 1:13: illegal char illegal

Note: the file /home/Username/Desktop/Test is the Demo Cloudformation stack in README.md

installation issue.

vagrant@vagrantbox-ubuntu:/terraformcf2tf$ node -v
v6.0.0
vagrant@vagrantbox-ubuntu:
/terraformcf2tf$ BASE_DIR=/home/vagrant/terraformcf2tf EXCLUDE_REGEX="(some-stack-production| some-stack-staging)" ./region-batch.sh us-east-1
Creating /home/vagrant/terraformcf2tf/us-east-1
Creating Terraform configuration for us-east-1
Creating config.tf with AWS provider information
Processing Docker
Setting AWS_REGION to us-east-1
Creating /home/vagrant/terraformcf2tf/us-east-1/Docker
Symlinking config.tf
Generating config for Docker
/usr/bin/env: ‘node --harmony’: No such file or directory
/usr/bin/env: ‘node --harmony’: No such file or directory
unable to parse JSON: EOF token found
vagrant@vagrantbox-ubuntu:~/terraformcf2tf$ grep -ir env *
batch.sh:#! /usr/bin/env bash
batch.sh: # Make sure AWS_PROFILE is set in your environment. This will be used to
import.sh:#! /usr/bin/env bash
import-stack.sh:#! /usr/bin/env bash
install_nvm.sh:#!/usr/bin/env bash
install_nvm.sh:# Detect profile file if not specified as environment variable
install_nvm.sh:[ "_$NVM_ENV" = "_testing" ] || nvm_do_install
region-batch.sh:#! /usr/bin/env bash
stack-compare.sh:#! /usr/bin/env bash
us-east-1/config.tf: # Make sure AWS_PROFILE is set in your environment. This will be used to

Type Error when i run cf-to-tf

I have a CFormation config file on my local directory in the path below. When i try to use cf-to-tf to convert using the command below. I get the below error message.

cf-to-tf --stack - config < /home/Username/Desktop/Test

TypeError: Cannot read property '0' of undefined
at getStack.then.result (/usr/local/lib/node_modules/@humanmade/cf-to-tf/index.js:47:54)
at
at process._tickDomainCallback (internal/process/next_tick.js:228:7)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.