diff --git a/tool/README.md b/tool/README.md index 45406af..0f83fcd 100644 --- a/tool/README.md +++ b/tool/README.md @@ -38,7 +38,7 @@ node -v * Terraform -You can run this application on [Alibaba cloud ECS](https://www.alibabacloud.com/help/doc-detail/25367.htm). If you want to run it on ECS, you don't have to install npm or node, but you have to install Terraform on your local machine, which is powerful tool to construct cloud infrastructure. You can install Terraform from [here](https://www.terraform.io/). +You can run this application on [Alibaba Cloud ECS](https://www.alibabacloud.com/help/doc-detail/25367.htm). If you want to run it on ECS, you don't have to install npm or node, but you have to install Terraform on your local machine, which is powerful tool to construct cloud infrastructure. You can install Terraform from [here](https://www.terraform.io/). You can check your environment with the following command in a terminal: @@ -46,23 +46,18 @@ You can check your environment with the following command in a terminal: terraform -version ``` -Before creating the application, you need to prepare account credentials of Alibaba cloud. +Before creating the application, you need to prepare account credentials of Alibaba Cloud. -0. Create an Alibaba cloud account +1. Create an Alibaba Cloud account - You need an Alibaba cloud account. If you don't have any account, please follow + You need an Alibaba Cloud account. If you don't have any account, please follow [this document to create one](https://www.alibabacloud.com/help/doc-detail/50482.htm). -1. Create an access key +2. Create an access key - You need an accessKeyId and an accessKeySecret to create your Alibaba cloud products by Terraform. Please follow + You need an accessKeyId and an accessKeySecret to create your Alibaba Cloud products by Terraform. Please follow [this document to obtain an access key id and secret](https://www.alibabacloud.com/help/faq-detail/63482.htm). -2. Create a SSH key - - You need a SSH key pair to access to your ECS instance by Terraform. Please follow - [this document to obtain an SSH key pair](https://www.alibabacloud.com/help/doc-detail/51793.htm). Please note the default target region of this sample is China(Qingdao). - ## Usage @@ -119,19 +114,19 @@ Before creating the application, you need to prepare account credentials of Alib 2. Edit the file to set your information ```sh - # Access key id of Alibaba Cloud account + # The access key id of the Alibaba Cloud account access_key = "xxxxxxxxxxxxxxxx" - # Access key secret of Alibaba Cloud account + # The access key secret of the Alibaba Cloud account secret_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" - # SSH key name of Alibaba Cloud ECS - ssh_key_name = "xxxxxxx" - # Local path to SSH key of Alibaba Cloud ECS - ssh_key_local_path = "////xxxxxxx.pem" + # The ssh key name of the Alibaba Cloud ECS + ssh_key_name = "ssh-increment-meta" + # The file path of the ssh key which will be saved on local + ssh_key_local_path = "./ssh-increment-meta-tf.pem" # Region in Alibaba Cloud region = "cn-qingdao" - # Prefix for name of ECS + # Prefix for the name of the ECS prefix = "tf-sample-" - # Suffix for name of ECS + # Suffix for the name of the ECS suffix = "-incremant-meta" ``` @@ -151,7 +146,7 @@ Before creating the application, you need to prepare account credentials of Alib terraform apply ``` - You can check the created product on Alibaba cloud from the following web console url. If you changed region settings in step 2, please change region accordingly. + You can check the created product on Alibaba Cloud from the following web console url. If you changed region settings in step 2, please change region accordingly. * ECS diff --git a/tool/infrastructure/.gitignore b/tool/infrastructure/.gitignore index f2a6df4..1f89c7e 100644 --- a/tool/infrastructure/.gitignore +++ b/tool/infrastructure/.gitignore @@ -1,3 +1,4 @@ .terraform* terraform.tfstate* -terraform.tfvars \ No newline at end of file +terraform.tfvars +*.pem diff --git a/tool/infrastructure/main.tf b/tool/infrastructure/main.tf index 7f8b156..72cfeaf 100644 --- a/tool/infrastructure/main.tf +++ b/tool/infrastructure/main.tf @@ -57,6 +57,11 @@ resource "alicloud_security_group_rule" "sgr22" { cidr_ip = "0.0.0.0/0" } +# SSH key pair +resource "alicloud_key_pair" "keypair_ecs1" { + key_name = "${var.ssh_key_name}" + key_file = "${var.ssh_key_local_path}" +} # ECS data "alicloud_images" "centos" { name_regex = "^centos_7.*vhd$" @@ -76,7 +81,8 @@ resource "alicloud_instance" "ecs1" { image_id = "${data.alicloud_images.centos.images.0.id}" instance_type = "${data.alicloud_instance_types.2c4g.instance_types.0.id}" instance_name = "${var.prefix}ecs1${var.suffix}" - key_name = "${var.ssh_key_name}" + + key_name = "${alicloud_key_pair.keypair_ecs1.key_name}" } # EIP (ecs1) resource "alicloud_eip" "eip_ecs1" { @@ -92,7 +98,7 @@ resource "alicloud_eip_association" "eip_ecs1_asso" { connection { type = "ssh" user = "root" - private_key = "${file(var.ssh_key_local_path)}" + private_key = "${file(alicloud_key_pair.keypair_ecs1.key_file)}" host = "${alicloud_eip.eip_ecs1.ip_address}" timeout = "1m" } @@ -105,7 +111,7 @@ resource "alicloud_eip_association" "eip_ecs1_asso" { connection { type = "ssh" user = "root" - private_key = "${file(var.ssh_key_local_path)}" + private_key = "${file(alicloud_key_pair.keypair_ecs1.key_file)}" host = "${alicloud_eip.eip_ecs1.ip_address}" timeout = "1m" } @@ -126,3 +132,6 @@ output "[debug] ecs1 image_id" { output "[debug] eip_ecs1 ip_address" { value = "${alicloud_eip.eip_ecs1.ip_address}" } +output "[output] ecs1 keypair_ecs1 key_name" { + value = "${alicloud_key_pair.keypair_ecs1.key_name}" +}