赞
踩
环境CentOS7.9
打开https://www.terraform.io/downloads.html,复制下载地址
wget https://releases.hashicorp.com/terraform/0.15.4/terraform_0.15.4_linux_amd64.zip
解压后,移动到/bin目录
unzip terraform_0.15.4_linux_amd64.zip
mv terraform /bin/
安装awscli
yum install awscli -y
配置awscli,输入id,key,地区和输出格式信息
aws configure
AWS Access Key ID [****************65UO]: ^C
[root@192-168-64-138 aws]# aws configure
AWS Access Key ID [****************65UO]:
AWS Secret Access Key [****************iOBH]:
Default region name [ap-northeast-2]:
Default output format [json]:
以下main.tf文件对于t2.micro是完全适用得,需要自己修改region,vpc_security_group_ids,subnet_id
aws自有ami镜像获取方式如下:
aws ec2 describe-images --owners self amazon
#mkdir aws #cd aws #vim main.tf terraform { required_providers { aws = { source = "hashicorp/aws" version = "~> 3.27" } } required_version = ">= 0.14.9" } provider "aws" { profile = "default" region = "ap-northeast-2" } resource "aws_instance" "app_server" { ami = "ami-0005600074f3aa4be" instance_type = "t2.micro" vpc_security_group_ids=["sg-066b80c1d6a8d170c"] subnet_id="subnet-0a792e831feab123a" tags = { Name = "ExampleAppServerInstance" } }
首先需要init目录
#terraform init Initializing the backend... Initializing provider plugins... - Reusing previous version of hashicorp/aws from the dependency lock file - Using previously-installed hashicorp/aws v3.42.0 Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure. All Terraform commands should now work. If you ever set or change modules or backend configuration for Terraform, rerun this command to reinitialize your working directory. If you forget, other commands will detect it and remind you to do so if necessary.
会看到目录下安装了aws的provider模块
创建
#terraform apply Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: # aws_instance.app_server will be created + resource "aws_instance" "app_server" { + ami = "ami-0005600074f3aa4be" + arn = (known after apply) + associate_public_ip_address = (known after apply) + availability_zone = (known after apply) + cpu_core_count = (known after apply) + cpu_threads_per_core = (known after apply) + get_password_data = false + host_id = (known after apply) + id = (known after apply) + instance_initiated_shutdown_behavior = (known after apply) + instance_state = (known after apply) + instance_type = "t2.micro" + ipv6_address_count = (known after apply) + ipv6_addresses = (known after apply) + key_name = (known after apply) + outpost_arn = (known after apply) + password_data = (known after apply) + placement_group = (known after apply) + primary_network_interface_id = (known after apply) + private_dns = (known after apply) + private_ip = (known after apply) + public_dns = (known after apply) + public_ip = (known after apply) + secondary_private_ips = (known after apply) + security_groups = (known after apply) + source_dest_check = true + subnet_id = "subnet-0a792e831feab123a" + tags = { + "Name" = "ExampleAppServerInstance" } + tags_all = { + "Name" = "ExampleAppServerInstance" } + tenancy = (known after apply) + vpc_security_group_ids = [ + "sg-066b80c1d6a8d170c", ] + capacity_reservation_specification { + capacity_reservation_preference = (known after apply) + capacity_reservation_target { + capacity_reservation_id = (known after apply) } } + ebs_block_device { + delete_on_termination = (known after apply) + device_name = (known after apply) + encrypted = (known after apply) + iops = (known after apply) + kms_key_id = (known after apply) + snapshot_id = (known after apply) + tags = (known after apply) + throughput = (known after apply) + volume_id = (known after apply) + volume_size = (known after apply) + volume_type = (known after apply) } + enclave_options { + enabled = (known after apply) } + ephemeral_block_device { + device_name = (known after apply) + no_device = (known after apply) + virtual_name = (known after apply) } + metadata_options { + http_endpoint = (known after apply) + http_put_response_hop_limit = (known after apply) + http_tokens = (known after apply) } + network_interface { + delete_on_termination = (known after apply) + device_index = (known after apply) + network_interface_id = (known after apply) } + root_block_device { + delete_on_termination = (known after apply) + device_name = (known after apply) + encrypted = (known after apply) + iops = (known after apply) + kms_key_id = (known after apply) + tags = (known after apply) + throughput = (known after apply) + volume_id = (known after apply) + volume_size = (known after apply) + volume_type = (known after apply) } } Plan: 1 to add, 0 to change, 0 to destroy. Do you want to perform these actions? Terraform will perform the actions described above. Only 'yes' will be accepted to approve. Enter a value: yes aws_instance.app_server: Creating... aws_instance.app_server: Still creating... [10s elapsed] aws_instance.app_server: Still creating... [20s elapsed] aws_instance.app_server: Still creating... [30s elapsed] aws_instance.app_server: Still creating... [40s elapsed] aws_instance.app_server: Creation complete after 43s [id=i-0cf62b32bec46f151] Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
在控制台可以看到生成ecs实例
#terraform destroy aws_instance.app_server: Refreshing state... [id=i-0cf62b32bec46f151] Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: - destroy Terraform will perform the following actions: # aws_instance.app_server will be destroyed - resource "aws_instance" "app_server" { - ami = "ami-0005600074f3aa4be" -> null - arn = "arn:aws:ec2:ap-northeast-2:506783418448:instance/i-0cf62b32bec46f151" -> null - associate_public_ip_address = true -> null - availability_zone = "ap-northeast-2a" -> null - cpu_core_count = 1 -> null - cpu_threads_per_core = 1 -> null - disable_api_termination = false -> null - ebs_optimized = false -> null - get_password_data = false -> null - hibernation = false -> null - id = "i-0cf62b32bec46f151" -> null - instance_initiated_shutdown_behavior = "stop" -> null - instance_state = "running" -> null - instance_type = "t2.micro" -> null - ipv6_address_count = 0 -> null - ipv6_addresses = [] -> null - monitoring = false -> null - primary_network_interface_id = "eni-0e51d97e2f38ecc57" -> null - private_dns = "ip-10-1-2-228.ap-northeast-2.compute.internal" -> null - private_ip = "10.1.2.228" -> null - public_ip = "3.35.238.62" -> null - secondary_private_ips = [] -> null - security_groups = [] -> null - source_dest_check = true -> null - subnet_id = "subnet-0a792e831feab123a" -> null - tags = { - "Name" = "ExampleAppServerInstance" } -> null - tags_all = { - "Name" = "ExampleAppServerInstance" } -> null - tenancy = "default" -> null - vpc_security_group_ids = [ - "sg-066b80c1d6a8d170c", ] -> null - capacity_reservation_specification { - capacity_reservation_preference = "open" -> null } - credit_specification { - cpu_credits = "standard" -> null } - ebs_block_device { - delete_on_termination = true -> null - device_name = "/dev/xvdcz" -> null - encrypted = false -> null - iops = 100 -> null - tags = {} -> null - throughput = 0 -> null - volume_id = "vol-0c313f374ca42cfe0" -> null - volume_size = 22 -> null - volume_type = "gp2" -> null } - enclave_options { - enabled = false -> null } - metadata_options { - http_endpoint = "enabled" -> null - http_put_response_hop_limit = 1 -> null - http_tokens = "optional" -> null } - root_block_device { - delete_on_termination = true -> null - device_name = "/dev/xvda" -> null - encrypted = false -> null - iops = 100 -> null - tags = {} -> null - throughput = 0 -> null - volume_id = "vol-04fb2bb0cb84c5ad4" -> null - volume_size = 8 -> null - volume_type = "gp2" -> null } } Plan: 0 to add, 0 to change, 1 to destroy. Do you really want to destroy all resources? Terraform will destroy all your managed infrastructure, as shown above. There is no undo. Only 'yes' will be accepted to confirm. Enter a value: yes aws_instance.app_server: Destroying... [id=i-0cf62b32bec46f151] aws_instance.app_server: Still destroying... [id=i-0cf62b32bec46f151, 10s elapsed] aws_instance.app_server: Still destroying... [id=i-0cf62b32bec46f151, 20s elapsed] aws_instance.app_server: Still destroying... [id=i-0cf62b32bec46f151, 30s elapsed] aws_instance.app_server: Still destroying... [id=i-0cf62b32bec46f151, 40s elapsed] aws_instance.app_server: Destruction complete after 46s Destroy complete! Resources: 1 destroyed.
查看控制台
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。