【Terraform学习】使用 Terraform 将 EC2 实例作为 Web 服务器启动(Terraform-AWS最佳实战学习)

使用 Terraform 将 EC2 实例作为 Web 服务器启动

实验步骤

前提条件

下载仓库代码模版

  • 本实验代码位于 task_ec2 文件夹中

  • 变量文件 variables.tf 

 

 

  • 在上面的代码中,您将声明aws_access_keyaws_secret_key和 区域变量

  • terraform.tfvars 

 

  • 在上面的代码中,您将定义变量的值

  • main.tf

 

 

  • 在上面的代码中,您将执行以下任务

    • 定义 Amazon Linux 2 AMI

    • 提到使用哪个 SSH 密钥对(已存在于您的 AWS EC2 控制台中)。安全组 ID 是使用将在创建过程中设置的变量自动获取的

    • 添加了用户数据来安装 Apache 服务器

    • 为 EC2 实例提供了标签

  • outputs.tf,将输出EC2 实例的公有 IP

 

 


使用 Terraform 部署

  • 确保您这些文件在同一位置

  • 通过运行以下命令初始化 Terraform

    • terraform init

    • 注意:terraform init检查所有插件依赖项下载它们

  • 查看生成计划,请运行以下命令

    • terraform plan

  • 创建在 main.tf 配置文件中声明的所有资源 ,请运行以下命令

    • terraform apply

  • 您将能够看到将要创建的资源,通过输入 yes 批准所有资源的创建

  • terraform apply 命令最多可能需要 2 分钟才能创建资源


检查 AWS 控制台中的资源

  • 确保您位于美国东部(弗吉尼亚北部)us-east-1 区域

  • 通过单击 AWS 控制台顶部导航到 EC2

  • 导航到左侧面板“网络与安全”下的安全组

  • 您将能够看到安全组名称为Terraform_Sg的安全组。

  

  • 导航到左侧面板上“实例”下的“实例”

  • 您可以看到创建的实例。您可以检查我们在 terraform 文件中应用的配置,例如密钥对、安全组、实例类型等。

  


检查 HTML 页面

  • 在 terraform 文件中,我们使用用户数据创建了一个 Apache 服务器并发布了一个 HTML 页面

  • 在浏览器中打开一个新选项卡,然后粘贴所创建的 EC2 实例的公有 IP

  • 在用户数据中创建的 HTML 内容将显示在页面中

  

  • 现在,我们可以说 EC2 实例已安装 Apache 服务器,并且 HTML 内容已正确发布

  • 我们还可以确认安全组允许 HTTP 传入请求

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用 TerraformEC2 模块来创建 EC2 实例。以下是一个示例: 1. 首先,在 main.tf 文件中定义一个 EC2 实例模块,例如: ``` module "ec2_instance" { source = "terraform-aws-modules/ec2-instance/aws" version = "~> 2.0" name = "example-instance" instance_count = 1 ami = "ami-0c55b159cbfafe1f0" instance_type = "t2.micro" key_name = "example-key" subnet_id = "subnet-1234567890abcdef0" vpc_security_group_ids = ["sg-1234567890abcdef0"] tags = { Terraform = "true" Environment = "dev" } } ``` 2. 在 variables.tf 文件中定义所需的变量,例如: ``` variable "name" { description = "The name of the EC2 instance" type = string } variable "instance_count" { description = "The number of EC2 instances to launch" type = number } variable "ami" { description = "The ID of the AMI to use for the EC2 instances" type = string } variable "instance_type" { description = "The instance type to use for the EC2 instances" type = string } variable "key_name" { description = "The name of the key pair to use for the EC2 instances" type = string } variable "subnet_id" { description = "The ID of the subnet in which to launch the EC2 instances" type = string } variable "vpc_security_group_ids" { description = "The IDs of the security groups to associate with the EC2 instances" type = list(string) } variable "tags" { description = "A map of tags to apply to the EC2 instances" type = map(string) } ``` 3. 运行 `terraform init` 命令初始化 Terraform 配置。 4. 运行 `terraform apply` 命令创建 EC2 实例模块。 以上就是使用 TerraformEC2 模块创建 EC2 实例的基本步骤。你可以根据自己的需求进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值