Giter Site home page Giter Site logo

gruntwork-io / intro-to-terraform Goto Github PK

View Code? Open in Web Editor NEW
731.0 60.0 531.0 103 KB

Sample code for the blog post series "A Comprehensive Guide to Terraform."

Home Page: https://blog.gruntwork.io/a-comprehensive-guide-to-terraform-b3d32832baca

License: Other

HCL 97.51% Go 2.49%
terraform devops samples aws

intro-to-terraform's Introduction

Maintained by Gruntwork.io Terraform Version

NOTE: This repo is now archived and replaced!

This repo contains the sample code for the blog post series A Comprehensive Guide to Terraform, which we turned into the book Terraform: Up & Running, with its own code examples repo.

Instead of maintaining two examples repos, we have decided to maintain just one. This repo is now archived, so please head over to the terraform-up-and-running-code repo for the most up to date examples.

Original content (saved for posterity)

An Introduction to Terraform Sample Code

This repo contains the sample code for the blog post series A Comprehensive Guide to Terraform. The examples correspond to the following parts of the series:

  1. An Introduction to Terraform
    • single-web-server: Deploy a single EC2 Instance with a web server that will return "Hello, World" for every request on port 8080.
    • cluster-of-web-servers: Deploy a cluster of EC2 Instances in an Auto Scaling Group (ASG) and an Elastic Load Balancer (ELB). The ELB listens on port 80 and distributes load across the EC2 Instances, each of which runs the same "Hello, World" web server.
  2. How to Manage Terraform State
    • s3-backend: Create an S3 bucket and DynamoDB table to use as a Terraform backend.
    • database: Deploy MySQL on top of Amazon's Relational Database Service (RDS).
  3. How to create reusable infrastructure with Terraform modules
    • modules: Examples of reusable Terraform modules, including a module that can deploy a web server cluster on top of ASG with an ELB.
    • live: Examples of how to deploy different live environments (i.e., staging, production) using the code from the modules folder.
  4. Terraform tips & tricks: loops, if-statements, and pitfalls
    • loops-with-count: Examples of how to use the count parameters to "loop" over resources.
    • loops-with-for-each: Examples of how to use for_each to "loop" over inline blocks.
    • loops-with-for: Examples of how to use for to "loop" over individual values.

Quick start

Note: These examples deploy resources into your AWS account. Although all the resources should fall under the AWS Free Tier, it is not our responsibility if you are charged money for this.

  1. Install Terraform.
  2. Set your AWS credentials as the environment variables AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY.
  3. cd into one of the example folders.
  4. Run terraform init.
  5. Run terraform apply.
  6. After it's done deploying, the example will output URLs or IPs you can try out.
  7. To clean up and delete all resources after you're done, run terraform destroy.

License

Please see LICENSE.txt for details on how the code in this repo is licensed.

intro-to-terraform's People

Contributors

brikis98 avatar gruntwork-ci avatar infraredgirl avatar jmaciasluque avatar josh-padnick avatar letubert avatar rhoboat avatar robmorgan avatar ryouku avatar yorinasub17 avatar zackproser 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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

intro-to-terraform's Issues

Single-web-server raises error

simply running terraform apply in the single-web-server directory raises:

  • aws_instance.example: Error launching source instance: VPCResourceNotSpecified: The specified instance type can only be used in a VPC. A subnet ID or network interface ID is required to carry out the request.
    status code: 400, request id: 94b7cfbe...

cluster-of-web servers

I tried to use code for cluster-of-web-servers.
Few resources were created successfully, but not all.

I keep getting this error, do you know why this is happening. It says issue is with configuration not supported.

Error: Error applying plan:

1 error(s) occurred:

  • aws_autoscaling_group.example: 1 error(s) occurred:

  • aws_autoscaling_group.example: "tf-asg-20190328160913020900000002": Waiting up to 10m0s: Need at least 2 healthy instances in ASG, have 0. Most recent activity: {
    ActivityId: "5cc5a714-61e2-eba7-663b-20124631a347",
    AutoScalingGroupName: "tf-asg-20190328160913020900000002",
    Cause: "At 2019-03-28T16:18:27Z an instance was started in response to a difference between desired and actual capacity, increasing the capacity from 0 to 2.",
    Description: "Launching a new EC2 instance. Status Reason: The requested configuration is currently not supported. Please check the documentation for supported configurations. Launching EC2 instance failed.",
    Details: "{"Availability Zone":"us-west-1a"}",
    EndTime: 2019-03-28 16:18:29 +0000 UTC,
    Progress: 100,
    StartTime: 2019-03-28 16:18:29.178 +0000 UTC,
    StatusCode: "Failed",
    StatusMessage: "The requested configuration is currently not supported. Please check the documentation for supported configurations. Launching EC2 instance failed."
    }

Thanks,
Alok

cluster server issue

Hi,
I'm new to terraform and have been following your guide. I've got the single webserver code running but when I try and run the cluster web servers, I get the following errors. I'm pretty sure I've missed something out!

cluster-of-web-servers]$ terraform plan
Error configuring: 2 error(s) occurred:

  • aws_autoscaling_group.example: missing dependency: data.aws_availability_zones
  • aws_elb.example: missing dependency: data.aws_availability_zones

Can you point me in the right direction please? I did a git clone, set my own access/secret key and ran the terraform plan.

Regards

Syntax error in blog post?

I was experimenting with workspaces in https://blog.gruntwork.io/how-to-manage-terraform-state-28f5697e68fa (great set of blog posts btw, thanks!) and tried this piece of code:

resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = 
    terraform.workspace == "default" 
    ? "t2.medium" 
    : "t2.micro"
}

which gave me an error:

Error: Argument or block definition required

on main.tf line 29, in resource "aws_instance" "example":
29: terraform.workspace == "default"

An argument or block definition is required here. To set an argument, use the
equals sign "=" to introduce the argument value.

Modifying the above code to this fixed my issue:

resource "aws_instance" "example" {
    ami           = "ami-0c55b159cbfafe1f0"
    instance_type = "${
        terraform.workspace == "default"
        ? "t2.medium"
        : "t2.micro"
    }"
}

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.