介绍
由于我是使用udemy学习Terraform的,所以这是一份学习备忘录,总结了从Terraform环境搭建到AWS资源创建的所有内容。
目的
能够编码/自动化(Iac)基础设施建设
什么是讲师即代码 (IaC)?
通过编码基础设施建设来防止人为错误并提高质量和速度
IAC的优势
- 更容易复制环境
- 更改由代码管理,因此您可以使用 git 管理它们
关于 HashiCorp
- 公司由 Mitchell Hashimoto 和 Armon Dadgar 于 2012 年创立
- 开发了
Terraform这次使用
什么是 Terraform
- Terraform 是一种启用 IaC 的工具
- Terraform 允许您在代码中声明您的基础架构配置并自动部署它。
- 支持多种云服务(GCP、Azure、AWS)
环境
安装 AWS CLI
首先安装 AWS CLI。
由于安装方法有官方文档,请参考官方安装。
安装完成后,您将能够使用 aws 命令。
$ aws --version
$ aws-cli/2.8.2 Python/3.10.7 Darwin/21.6.0 source/x86_64 prompt/off
IAM 用户创建
在 IAM on AWS 中为 Terraform 创建一个 IAM 用户。
对于名称,选择访问密钥程序。
授予管理员和创建 IAM 用户的权限。 (标签等随意给出)
创建用户后,设置 aws configure。
$ aws configure --profile terraform
AWS Access Key ID [None]:作成したもの
AWS Secret Access Key [None]:作成したもの
Default region name [None]:ap-northeast-1
Default output format [None]:json
如果可以看到 S3 存储桶,请指定配置文件并确定
$ aws s3 ls --profile terraform
安装 Terraform
安装 tfenv,Terraform 的版本管理工具,因为 Terraform 经常会根据版本而变化。
安装 tfenv
我正在使用 anyanv,所以我通过 anyenv 安装 tfenv。
# tfenv をインストール
$ anyenv install tfenv
# コマンドを実行して正常にインストールできているか確認
$ tfenv
tfenv 3.0.0-18-g1ccfddb
Usage: tfenv <command> [<options>]
也可以用 brew 安装。
$ brew install tfenv
安装 Terraform
显示 Terraform 的可安装版本。
$ tfenv list-remote
安装最新的。
$ tfenv install 1.3.2
$ tfenv use 1.3.2
# バージョン確認でインストールが成功したことを確認
$ terraform version
Terraform v1.3.2
on darwin_amd64
安装 git-secrets
一种防止您意外提交 AWS 访问密钥和秘密密钥的工具。
$ brew install git-secrets
通过为每个 git 存储库运行 git secrets --install 来启用 git-secrets。
$ git secrets --install
# AWSのIAMで利用されているクレデンシャルのパターンを弾く専用のオプションを設定します。
$ git secrets --register-aws
安装 VSCode 插件
安装 HashiCorp 提供的插件。
开始使用 Terraform
创建 EC2
创建一个新文件夹并创建一个 main.tf 文件。
# プロバイダーの設定
provider "aws" {
profile = "terraform"
region = "ap-northeast-1"
}
# EC2の設定
# amiは、awsのコンソールから確認します。
resource "aws_instance" "hello-world" {
ami = "ami-0ce107ae7af2e92b5"
instance_type = "t2.micro"
}
初始化以便可以使用 terraform。
$ terraform init
运行地形。
$ terraform apply
检查AWS控制台,如果创建了EC2就OK了。
什么是 tfstate
$ terraform apply 将创建 terraform.tfstate 文件。
tfstate 文件记录了使用 terraform 构建的云的所有状态信息。
查看之前创建的 tfstate,创建的 ec2 的 id 被写入,并且与 AWS 上启动的 EC2 的 id 匹配。
为 EC2 添加标签(无需重新创建)
将标签添加到您之前编写的代码并添加 EC2 标签。
provider "aws" {
profile = "terraform"
region = "ap-northeast-1"
}
resource "aws_instance" "hello-world" {
ami = "ami-0ce107ae7af2e92b5"
instance_type = "t2.micro"
tags = {
Name = "HelloWorld"
}
}
您可以看到通过运行 terraform apply 添加的标签。
$ terraform apply
在 EC2 上安装 NGINX(重新创建)
将 nginx 添加到您的用户数据中。 @987654341 @描述nginx的安装。
provider "aws" {
profile = "terraform"
region = "ap-northeast-1"
}
resource "aws_instance" "hello-world" {
ami = "ami-0ce107ae7af2e92b5"
instance_type = "t2.micro"
tags = {
Name = "HelloWorld"
}
user_data = <<EOF
#!/bin/bash
amazon-linux-extras install -y nginx1.12
systemctl start nginx
EOF
}
删除 EC2
通过执行以下命令,可以删除 terraform 创建的资源。
$ terraform destroy
查看 AWS 控制台,您可以确认之前创建的 EC2 已被删除。
基本命令
# 実行計画の確認
$ terraform plan
# 変更の適用
$ terraform apply
# 管理インフラ環境の構築
$ terraform destroy
# 構文チェック
$ terraform validate
# フォーマット
$ terraform fmt
就这样。您可以使用 Terraform 轻松创建云资源。
原创声明:本文系作者授权爱码网发表,未经许可,不得转载;
原文地址:https://www.likecs.com/show-308629449.html