4
votes

I am using Terraform for most of my infrastructure, but at the same time I'm using the serverless framework to define some Lambda functions. Serverless uses CloudFormation under the hood where I need access to some ARNs for resources created by Terraform.

My idea was to create a CloudFormation stack in Terraform and export all of the value that I need, but it complains that it cannot create a stack without any resources. I don't want to define any resources in CloudFormation, only the outputs, so I though maybe there is a way to define some dummy resource, but I couldn't find any.

Is there a way to work around this issue? If not, I'm also open to other suggestions for getting parameters passed from Terraform to CloudFormation.

3

3 Answers

6
votes

The Resource section is required, but you can create non-resource type of resource.

For example, minimalist template with only a non-resource would be:

Conditions:

  Never:
    !Equals [ "A", "B" ]

Resources:

  NonResource:
    Type: Custom::NonResource
    Condition: Never

Outputs:
  
  MyOutput:
    Value: some-value
  
4
votes

You can use AWS::CloudFormation::WaitConditionHandle for this. Example:

Resources:
  NullResource:
    Type: AWS::CloudFormation::WaitConditionHandle
0
votes

CloudFormation stacks require at least one resource to be made (after all the purpose of CloudFormation is to deploy resources programmatically).

CloudFormation supports parameters which you can pass into the stack.

In Terraform you can use these parameters like in the example below.

resource "aws_cloudformation_stack" "network" {
  name = "networking-stack"

  parameters = {
    VPCCidr = "10.0.0.0/16"
  }

  template_body = <<STACK
{
  "Parameters" : {
    "VPCCidr" : {
      "Type" : "String",
      "Default" : "10.0.0.0/16",
      "Description" : "Enter the CIDR block for the VPC. Default is 10.0.0.0/16."
    }
  },
  "Resources" : {
    "myVpc": {
      "Type" : "AWS::EC2::VPC",
      "Properties" : {
        "CidrBlock" : { "Ref" : "VPCCidr" },
        "Tags" : [
          {"Key": "Name", "Value": "Primary_CF_VPC"}
        ]
      }
    }
  }
}
STACK
}

There is a post on using the serverless framework with Terraform and some examples on GitHub of how to set it up.