使用Terraform,遵循文档这里和那里,我尝试为ec2实例创建一个模板,并使用弹性推理gpu规范和图形。
这是aws启动模板资源的我的代码:
resource "aws_launch_template" "elastic_ec2" {
name_prefix = "DeepLearning"
description = "Deep Learning"
disable_api_termination = true
ebs_optimized = true
image_id = "ami-0d9d11b8557309342"
instance_initiated_shutdown_behavior = "terminate"
instance_type = "t3.medium"
key_name = "${local.pem_key_name}"
block_device_mappings {
device_name = "/dev/sda1"
}
capacity_reservation_specification {
capacity_reservation_preference = "open"
}
credit_specification {
cpu_credits = "standard"
}
elastic_gpu_specifications {
type = "eg1.medium"
}
elastic_inference_accelerator {
type = "eia1.medium"
}
iam_instance_profile {
name = "my-right-profile"
}
instance_market_options {
market_type = "spot"
}
monitoring {
enabled = true
}
network_interfaces {
associate_public_ip_address = true
}
placement {
availability_zone = "${var.main_location}"
}
tag_specifications {
resource_type = "instance"
tags = {
Environment = "${local.environment}"
}
}
}
resource "aws_instance" "web" {
ami = "${aws_launch_template.elastic_ec2.image_id}"
instance_type = "${aws_launch_template.elastic_ec2.instance_type}"
key_name = "${local.pem_key_name}"
iam_instance_profile = "${aws_iam_instance_profile.ec2_profile.name}"
tags = {
Environment = "${local.environment}"
App = "${local.app_name}"
}
}当然,这是用于按照doc规范这里部署实例的策略。
resource "aws_iam_role" "ec2_exec_role" {
name = "ec2_exec"
path = "/"
description = "Allows Lambda Function to call AWS services on your behalf."
assume_role_policy = <<POLICY
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": [
"ec2.amazonaws.com",
"ecs-tasks.amazonaws.com"
]
},
"Effect": "Allow",
"Sid": ""
}
]
}
POLICY
}
resource "aws_iam_instance_profile" "ec2_profile" {
name = "${local.environment}_ec2_profile"
role = "${aws_iam_role.ec2_exec_role.name}"
}
resource "aws_iam_policy" "ec2_policy" {
name = "${local.environment}_ec2_policy"
description = "Ec2 main polify for ${local.environment} environment"
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"elastic-inference:Connect",
"iam:List*",
"iam:Get*",
"ec2:Describe*",
"ec2:Get*",
"ecs:RegisterTaskDefinition",
"ecs:RunTask"
],
"Resource": [
"arn:aws:ec2:${var.main_location}::*",
"arn:aws:elastic-inference:${var.main_location}:${data.aws_caller_identity.current.account_id}:*",
"arn:aws:iam::${data.aws_caller_identity.current.account_id}:role/ecs-ei-task-role",
"arn:aws:iam::${data.aws_caller_identity.current.account_id}:role/ecsTaskExecutionRole"
]
}
]
}
EOF
}
resource "aws_iam_policy_attachment" "ec2_attachment" {
name = "${local.environment}_ec2__attachment"
roles = ["${aws_iam_role.ec2_exec_role.name}"]
policy_arn = "${aws_iam_policy.ec2_policy.arn}"
}当我使用命令terraform plan和terraform apply部署terraform代码时,没有出错。我已经部署了我的实例模板和我的ec2实例,但是我可以看到,在我的实例中,我没有附加弹性推理加速器ID。正确的配置是什么?我需要使用terraform来正确地附加所有弹性推理规范。
发布于 2019-11-17 02:25:42
谢谢你提出这个问题,它让我回答了如何用弹性推理加速器启动一个EC2实例。目前,AWS terraform提供者回购中存在一个关于添加一个选项,直接将EI附加到EC2实例的问题。
此配置的问题是,启动模板与您的实例没有关联。I通过在自动定标组的启动配置中指定启动模板,能够使用EI启动EC2实例。
但是,请记住,要将EI附加到EC2实例,还需要执行许多其他步骤。指定安全组,创建VPC端点.文档
希望这能有所帮助。
https://stackoverflow.com/questions/58271771
复制相似问题