top of page
download.png

CloudMates Blog

How to integrate AWS Cloudformation with Terraform

AWS Cloudformation and Terraform are among the most common used IaC platforms to provision resources with the first used solely within AWS cloud ecosystem. Speaking of AWS cloud, there are occasions where you might have a mix of Terraform modules and Cloudformation templates doing particular functions due to non-standardized environments and diverse skillsets where some engineers prefers one platform over another and you want to leverage both at the same time without transforming one to another. So the question becomes, can we not reinvent the wheel and mix the use of both platforms at the same time to provision the desired resources without the hassle of transformation?


The answer is yes and the way we do that is by creating a Terraform resource "aws_cloudformation_stack" and provide the Cloudformation template either as a yml file or simply plain yml text as part of the template_body parameter. You can use Json format if you want but I prefer yml for clarity as it's more human readable.


Now Let's explore both options and see the effect. Below is a simple Cloudformation template that creates a sample Lambda Function and an IAM role for execution.

AWSTemplateFormatVersion: 2010-09-09
Description:
  Sample Lambda Function
Resources:
  TestLambdaRole:
    Type: AWS::IAM::Role
    Properties:
      RoleName: TestLambdaRole
      AssumeRolePolicyDocument:
        Statement:
          - Effect: Allow
            Principal:
              Service: lambda.amazonaws.com
            Action: sts:AssumeRole
                

  TestLambdaFunction:
    Type: AWS::Lambda::Function
    Properties:
      FunctionName: TestLambdaFunction
      Role: !GetAtt TestLambdaRole.Arn
      Runtime: python3.8
      Handler: index.my_handler
      Code:
        ZipFile: |
          def my_handler(event, context):
            message = 'Hello Lambda World!'
            return message

To use this CF template within Terraform, we can use the following code snippet:

resource "aws_cloudformation_stack" "cf" {
  name = "lambda-cf"
  capabilities = ["CAPABILITY_NAMED_IAM"]
  template_body = file("${path.module}/lambda.yml")
}

Essentially in line 4, we're referencing the CF template we created above named "lambda.yml".


Alternatively, you can simply put the yml code of the CF template directly in Terraform without creating a file and referencing it as we did in the previous method. An example:

resource "aws_cloudformation_stack" "cf" {
  name = "lambda-cf"
  capabilities = ["CAPABILITY_NAMED_IAM"]
template_body = <<STACK
AWSTemplateFormatVersion: 2010-09-09
Description:
  Sample Lambda Function
Resources:
  TestLambdaRole:
    Type: AWS::IAM::Role
    Properties:
      RoleName: TestLambdaRole
      AssumeRolePolicyDocument:
        Statement:
          - Effect: Allow
            Principal:
              Service: lambda.amazonaws.com
            Action: sts:AssumeRole
                

  TestLambdaFunction:
    Type: AWS::Lambda::Function
    Properties:
      FunctionName: TestLambdaFunction
      Role: !GetAtt TestLambdaRole.Arn
      Runtime: python3.8
      Handler: index.my_handler
      Code:
        ZipFile: |
          def my_handler(event, context):
            message = 'Hello Lambda World!'
            return message
STACK
}

Now when executing the command "terraform apply --auto-approve"



In Cloudformation Console, we can see the following events taking place:






0 comments

Recent Posts

See All

Comentarios


© CloudMates Business Solutions Pty Ltd 2024

bottom of page