Home > Software engineering >  How to access key value pairs stored in aws parameter store using terraform script
How to access key value pairs stored in aws parameter store using terraform script

Time:12-21

Requirement is that I want to pass my SSH key to the ec2 instance during runtime, which is stored in the parameter store using terraform code.

  data "aws_ssm_parameter" "key_pair" {                                                                          
 name = "/Test/keystest"    
} 

Here is my launch configuration

 resource "aws_launch_configuration" "app" {
image_id  = data.aws_ssm_parameter.ami.value
instance_type = "t2.micro"
key_name = data.aws_ssm_parameter.key_pair.value
security_groups = [ aws_security_group.sg_web.id ]
  associate_public_ip_address = true

  user_data = <<USER_DATA
#!/bin/bash
sudo su
yum update -y
yum install httpd -y
cd /var/www/html
echo "MyGoogle-2" > index.html
service httpd start
chkconfig httpd on
  USER_DATA

  lifecycle {
    create_before_destroy = false
  }
}

when i execute the above tf code i'm getting the below error

Error creating launch configuration: ValidationError: 1 validation error detected: 
Value 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDBBBnLT Q2ga26mv4coeobj4flEDtX/lfmP4tCWYhMW868UqHIJw4C Ns1yh3Ondp1sW094UR6NCJHKJeGkFrzB7/EaPKLt/z3wJceIsXKSsqS539YiaslIY54t7fDpM0qcE/Y6K zS21lGLEuAys/OwgutZGgFxDSDWtTleU0kRvnI4MVsPtWLMu4AzfvrlHrYkv2fGiwfJzq/UjnF TvHTPoYMp3TImjvhLzrmL1y2XrWesj7Q1E1xsgMgs4N5WIbXGI45KuRBcJVNmSpw/quv5vz/3NxtRQwmQcPriJo Fucj14 UsW CD3agbPn4arh4PE8E2Gel Test' at 'keyName' failed to satisfy constraint: Member must have length less than or equal to 255
│       

status code: 400, request id: f5bf125c-54b8-4bf5-ad13-aead7176f3a3


  

CodePudding user response:

The error means that your key_name is wrong. You are trying to pass an ssh key to it, but it should be a name, e.g. "MyKeyPair", which you create using aws_key_pair. This is where you specify your public_key.

  • Related