Loading

Unlocking the Power of Infrastructure as Code (IaC)

2025-01-30T13:07:29.600Z

RayRay

AWS Cloud Development Kit(CDK)

The AWS CDK is a higher-level framework that allows you to define your infrastructure programmatically using languages like TypeScript, Python, Java, or C#.

Comparison of AWS CDK vs. Alternatives

Tool Language Support Abstraction Level Deployment Method AWS Integration Learning Curve
AWS CDK TypeScript, Python, Java, C#, Go High-level (constructs) CloudFormation Native Moderate
AWS CloudFormation YAML/JSON Low-level (manual templates) AWS CLI or AWS Console Native Steep for large stacks
Terraform HCL (HashiCorp Language) Medium-level (modules) Terraform CLI Multi-cloud Moderate
Pulumi TypeScript, Python, Go, C# High-level (code-based) Pulumi CLI Multi-cloud Moderate
Ansible YAML Configuration-focused SSH/Agent-based Indirect Low to Moderate
Serverless Framework YAML/JSON, some code Function-driven Serverless CLI Native (Lambda focus) Moderate

Terrform vs CF

How Terraform Works

  • Direct API Calls: Terraform defines infrastructure as code using its own configuration language. When you run commands like terraform plan or terraform apply, Terraform communicates directly with AWS APIs to create, modify, or delete resources according to your configuration.

  • State Management: Terraform maintains state information in a file (terraform.tfstate) to track the resources it manages. This allows Terraform to know what changes need to be applied or undone in subsequent executions.

  • Independence from CloudFormation: Terraform operates independently of CloudFormation, meaning it does not rely on AWS CloudFormation to manage or organize resources. It does not create CloudFormation stacks; rather, it uses its configuration to interact with AWS.

How CloudFormation Works

  • Declarative Templates: AWS CloudFormation uses JSON or YAML templates to define AWS resources and their configuration. When a CloudFormation stack is created, AWS processes the template to automatically manage the lifecycle of its resources.

  • Stack Management: Resources defined by a CloudFormation template are grouped into a stack, which allows you to manage them together as a single unit. You can update, delete, or maintain these resources using CloudFormation commands or the AWS Management Console.

Key Differences

  • Operation: Terraform uses its own configurations to interact directly with AWS without involving CloudFormation stacks or templates.

  • State Tracking: Terraform handles and maintains its state independently, while CloudFormation inherently tracks resource states as part of its stack definition.

  • Flexibility & Tools: Terraform offers more flexibility in defining infrastructure beyond AWS and supports multi-cloud environments. CloudFormation is specifically tailored to AWS and all its features.

Terraform state management

The best practice is to manage state with Dynamo DB Benefits of Using a DynamoDB Table with S3 Backend State Locking:

DynamoDB provides state locking for your Terraform state files. This prevents multiple concurrent operations from corrupting your state by ensuring only one plan or apply operation can occur at a time.

Consistency:

Helps ensure that all changes are consistently applied to the state, reducing the risk of conflicts and errors that could arise from simultaneous updates.

Scalability:

DynamoDB is a highly scalable service that can handle the state locking for even the largest and most complex infrastructures managed by Terraform.

terraform init

Purpose: Initializes a Terraform working directory. This command is required to set up or reinitialize the resources Terraform needs to function. Actions Performed:

  • Backend Configuration: Sets up the configuration for remote backends, allowing Terraform to store state data remotely if configured.
  • Module Downloads: Downloads and installs modules specified in the configuration files.
  • Provider Plugins: Downloads the necessary provider plugins based on the specified versions in your configuration (e.g., the AWS provider).
  • State Migration: Manages state migration if any state-related configuration changes are introduced since the last initialization.

Output: Provides information about the setup process, any versions of providers that were configured, and confirms that Terraform is ready to use.

terraform plan

Purpose: Creates an execution plan that shows what actions Terraform will take to change infrastructure to match the configuration files. Actions Performed:

  • Resource Comparison: Compares the current state (from your state file) with your configuration files to determine what needs to be created, updated, or destroyed.
  • Drift Detection: Identifies and highlights any configuration drift between your actual infrastructure and the desired state defined in the configuration.
  • Dry Run: It's essentially a dry-run operation that doesn't change infrastructure, only displaying what it will do.

Output: Displays a detailed map of actions it plans to perform, including resource creations, modifications, and deletions. It helps you verify changes before applying.

Boostrap

  • Terrform deployment requires S3 and DynamoDB for the state and locking state persistence. So we need to create these separately with a different Terraform deployment stack, this typically happens in a separate directory with a bootstrap.tf specifying the S3 and DB to be created
  • After bootstrapping, then deploy terraform in the application infrastructure terraform directory to deploy
Contact Us
  • Office Address

    Melbourne, Australia

  • Email Us

    info@zdgroup.com.au

  • Call Us

    +61408985566

2025 © zdgroup.com.au. All rights reserved.  ZDAU Group