Skip to content

Instantly share code, notes, and snippets.

@ashwiniag
Created November 11, 2021 14:39
Show Gist options
  • Save ashwiniag/7f64508f0c4f3a4336b8ea9034d32764 to your computer and use it in GitHub Desktop.
Save ashwiniag/7f64508f0c4f3a4336b8ea9034d32764 to your computer and use it in GitHub Desktop.
Adds ssh key to aws secrets manager using terraform
provider "aws" {
region = "ap-south-1"
}
resource "tls_private_key" "instance" {
algorithm = "RSA"
}
resource "aws_key_pair" "instance" {
key_name = "test-keypair"
public_key = tls_private_key.instance.public_key_openssh
tags = {
Name = "test-keypair"
}
}
resource "aws_instance" "instance" {
ami = "<ami>"
instance_type = "t2.small"
key_name = aws_key_pair.instance.key_name
subnet_id = "<value>"
vpc_security_group_ids = ["<value>"]
tags = { env = "testing" }
}
# Creates and stores ssh key used creating an EC2 instance
resource "aws_secretsmanager_secret" "example" {
name = "example"
}
resource "aws_secretsmanager_secret_version" "example" {
secret_id = aws_secretsmanager_secret.example.id
secret_string = tls_private_key.instance.private_key_pem
}
# Output
output "instance_id" {
value = aws_instance.instance.id
}
output "secretsmanager_secret" {
value = aws_secretsmanager_secret.example.id
}
output "secretsmanager_secret_version" {
value = aws_secretsmanager_secret_version.example.id
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment