Skip to content

apprenticecto/create-aws-ec2-with-terraform

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 

Repository files navigation

This repo illustrates the basic steps to:

  • install or upgrade terraform (if already installed) on your dev Mac
  • create your AWS account, with free tier (if you don't have one yet)
  • configure, launch and then destroy an EC2 instance on AWS

Install or update Terraform on your Mac

  • install homebrew, a package manager, if not already done
  • install the HashiCorp tap, a repository of all our Homebrew packages, by entering brew tap hashicorp/tap
  • open your Terminal and enter $ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
  • enter $ brew install hashicorp/tap/terraform or brew update hashicorp/tap/terraform if already installed (check you version with terraform -v)
  • Check your installation ny entering terraform -v: you should get the just installed version
  • If you get an error that terraform could not be found, your PATH environment variable was not set up properly. Please go back and ensure that your PATH variable contains the directory where Terraform was installed.
  • See [herehere for detailed instructions.

Create or access your AWS Account

  • If don't have an AWS account, you can create one here: you have access to a 12 months free tier program

Install AWS CLI

  • If you already installed AWS CLI version1, follow the instructions here to uninstall it
  • Install the latest AWS CLI version 2 using MacOS command line by entering:
    • $ curl "https://awscli.amazonaws.com/AWSCLIV2.pkg" -o "AWSCLIV2.pkg"
    • sudo installer -pkg AWSCLIV2.pkg -target /
    • see detailed instructions here
  • Verify the installation:
    • enter $ which aws which should return "/usr/local/bin/aws"
    • enter $ aws --version which should return at least "aws-cli/2.0.61 Python/3.7.4 Darwin/19.6.0 exe/x86_64" (at the time of writign this file)
  • Detailed instructions and more options can be found here

Configure AWS CLI

Configure your AWS CLI with your AWS credentials (use your root if you just created your account, or an existing IAM user account):

  • enter $ aws configure
  • insert your account ID and Access Key ID, which you can find here
  • enter your default region
  • press enter for the default output format

Create and cd into a new folder which will contain your code

After creating and entering your folder, sync it with your remote repo (see this article to set it on Github).

You can fork this repo if you wish.

If you don't, do not forget to add a .gitignore file (see contents in this repo), to avoid versioning state information and large files, which might fail.

Create a IAM user

Following AWS best practices on IAM:

  • logon to your aws account
  • create an IAM user, with programmatic access (i.e. via AWS APIs)
  • create an 'admin' group with 'Full Access EC2' policy attached
  • add the user to the admin group

Create and destroy an EC2 instance

The file ec2_instance.tf contains the Terraform code to create (and then destroy) an ec2 instance, with a specific Amazon Machine Image.

Run terraform apply to create the specified instance on your AWS account. Type 'yes' when prompted, to confirm the creation.

Check on your aws console that the instance is actually running.

Run terraform destroy to create the specified instance on your AWS account. Type 'yes' when prompted, to confirm the destroy.

Again, you should check from your console, that your instance is terminated.

Commit and push your code to your remote repo

After successfully destroying your instance:

  • commit your code
  • push your committed code to your remote repo

About

How to create an AWS EC2 instance with Terraform

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages