While creating ec2 instance with Terraform, I wish to inject the whole pem key file into the script.
resource "aws_instance" "myinstance" {
...
user_data = base64encode(templatefile(
"${path.module}/userdata.sh", {
conf_tls_private_key = tls_private_key.mykey.private_key_pem
conf_tls_self_signed_cert = tls_self_signed_cert.mykey2.cert_pem
}
))
}
The terraform file implementation is as above. While using terraform console, I tested the tls_private_key.mykey.private_key_pem
expression and it gives me the normal OPENSSH PRIVATE KEY format.
❯ tf console
> tls_private_key.boundary.private_key_pem
-----BEGIN RSA PRIVATE KEY-----
MIIEogIBAAKCAQEAql5ImjYHynOkQGWL0DGT/3FGCIM6gouUi57MllyZCztzrBwp
FtWe+zJNWWjAu+WeyESqSnzmdFRnUiIqqo4Fq98yvF3+pjsbSMsC9s4H4wz/gp4o
ZzYXja4u7fBWGFMUKfU53M6zZS0FL0QQ/Tgt9u+p9q6HlyhtxiNKY0e3aX4xdCMH
QSrBLBIHLlpapkl/bvWk1xkSPEPNkNHbTvBd+3z2iWtxANEqO6ARItCCI+DcGyWP
W1FIwwKFBftNQAgxfs2pxdSnqpoaHSi4M9XjY7r7bADUItf9zpAy+PbqJr+F3Bzc
However, while the userdata.sh
gets executed in ec2, it gives an error like below.
/var/lib/cloud/instance/scripts/part-001: line 8: RSA: command not found
/var/lib/cloud/instance/scripts/part-001: line 9: MIIEogIBAAKCAQEAql5ImjYHynOkQGWL0DGT/3FGCIM6gouUi57MllyZCztzrBwp: No such file or directory
/var/lib/cloud/instance/scripts/part-001: line 10: FtWe+zJNWWjAu+WeyESqSnzmdFRnUiIqqo4Fq98yvF3+pjsbSMsC9s4H4wz/gp4o: No such file or directory
/var/lib/cloud/instance/scripts/part-001: line 11: ZzYXja4u7fBWGFMUKfU53M6zZS0FL0QQ/Tgt9u+p9q6HlyhtxiNKY0e3aX4xdCMH: No such file or directory
/var/lib/cloud/instance/scripts/part-001: line 12: QSrBLBIHLlpapkl/bvWk1xkSPEPNkNHbTvBd+3z2iWtxANEqO6ARItCCI+DcGyWP: No such file or directory
/var/lib/cloud/instance/scripts/part-001: line 13: W1FIwwKFBftNQAgxfs2pxdSnqpoaHSi4M9XjY7r7bADUItf9zpAy+PbqJr+F3Bzc: command not found
It seems that each line in file is interpreted as a command, which is not I’ve intended.
The code in userdata.sh
that caused the problem is as below.
echo $conf_tls_private_key | sudo tee "/etc/pki/tls/boundary/boundary.key"
Is there anything I’m unaware of or anything I can fix this problem? Thanks a lot !