Although servers and other resources can be manually created in the AWS background, the cost of the creation process is high. Therefore, terraform can be used to manage server resources, and the management of server resources can be written into files. The management of server resources can be realized by modifying files

Terraform support aws, aliyun, ucloud service providers, such as a complete list of service providers refer to: www.terraform.io/docs/provid…

Terraform is a command-line tool, download address: www.terraform.io/downloads.h…

Generate an API access key

To visit this page https://console.amazonaws.cn/iam/home?#/users to add a new user, type set to programmatic access

Configuring an Access Key

Write the aws obtained above to a TerraForm configuration file, such as config.tf

Provider "AWS" {access_key = "AWS accesskey" secret_key =" AWS secretkey" region = "AWS region code"}Copy the code

Aws code list: docs.aws.amazon.com/zh_cn/gener…

Then use the terraform init command to download the AWS plug-in as follows

Define the resources

Define the VPC

vim vpc.tf

Resource "aws_vpc" "myvpc" {cidr_block = "172.17.0.0/16" enable_dNS_hostnames = true Enable_DNS_support = true instance_tenancy = "default" tags = { Name = "myvpc" } }Copy the code

Use TerraForm Apply to create this resource on AWS

You can see that the VPC has been created successfully. You can confirm it on the AWS page

Note down the VPC ID: Vpc-054a32cba12D43EFe

The defined subnet

vim sn.tf

Resource "aws_subnet" "mysubnt-B1" {vpc_id = "Vpc-054a32cba12d43efe" cidr_block = "172.17.1.0/24" availability_zone = "cn-northwest-1b" map_public_ip_on_launch = false tags { Name = "mysubnt-b1" } }Copy the code

View operation Results

Confirm on this AWS interface

Define the instance

vim ec2.tf

resource "aws_instance" "myinstance" {
  ami           = "ami-0135cb179d33fbe3e"
  instance_type = "t2.medium"

  key_name = "dev"
  subnet_id = "subnet-0b3cebdaada76cfac"
  private_ip = "172.17.1.101"

  tags = {
        Name = "myinstance"}}Copy the code

This can also be confirmed on the AWS web page

Update the resource

For example, change the ec2 specification to: T2.small VIm EC2.tf The modified EC2.tf is as follows

resource "aws_instance" "myinstance" { ami = "ami-0135cb179d33fbe3e" instance_type = "t2.small" key_name = "dev" Subnet_id = "subnet-0b3cebdaada76cfac" private_ip = "172.17.1.101" tags = {Name = "myInstance"}}Copy the code

Then use the terraForm apply command to perform the changes

You can confirm this on the AWS Web interface

Delete the resource

You can use the command terraform destroy to delete all of the resources created above

The operation effect is as follows

Import the resource

The terraform can import existing resources, reference: www.terraform.io/docs/import…

A couple of points to note

  1. The Terraform is stateful, and *. State is the file that holds the state

Other resources to focus on are:

  1. Internet gateway
  2. NAT gateway

The file structure is as follows

The terraform configuration file can define a variable, reference: www.terraform.io/docs/config…

The resources

To define the load balancer reference: www.terraform.io/docs/provid…

  1. www.infoq.cn/article/9-r…
  2. www.terraform.io/docs/provid…
  3. www.terraform.io/docs/provid…
  4. www.terraform.io/docs/provid…
  5. www.terraform.io/downloads.h…
  6. Aws.amazon.com/cn/ec2/inst…
  7. www.terraform.io/docs/config…