In this lab because of lack of budged so i will only do 1 HSM in a cluster :)). I will do Manage the private keys of an issuing certificate authority (CA) locally. In process -> Encrypt and decrypt data with multiple cryptographic SDKs. JCE examples
Information
1. Create HSM resource
#create cluster
resource "aws_cloudhsm_v2_cluster" "cloudhsm_v2_cluster" {
hsm_type = "hsm1.medium"
subnet_ids = module.vpc.private_subnets
#security group cannot be define before create so it much manually config
tags = {
Name = "aws_cloudhsm_v2_cluster"
}
}
output "cluster_id" {
description = "CloudHSM V2 Cluster ID"
value = aws_cloudhsm_v2_cluster.cloudhsm_v2_cluster.cluster_id
}
2. Create HSM user

3. Initialize HSM cluster
#Create 1 hsm in subnet (10.0.1.0/24)
resource "aws_cloudhsm_v2_hsm" "cloudhsm_v2_hsm" {
subnet_id = module.vpc.private_subnets[0]
cluster_id = aws_cloudhsm_v2_cluster.cloudhsm_v2_cluster.cluster_id
}
output "hsm_id" {
description = "CloudHSM V2 HSM ID"
value = aws_cloudhsm_v2_hsm.cloudhsm_v2_hsm.id
}
4. Download Cluster CSR
export HSM_CLUSTER_ID=...
aws cloudhsmv2 describe-clusters \
--filters clusterIds=$HSM_CLUSTER_ID \
--output text --query 'Clusters[].Certificates.ClusterCsr' > myClusterCsr.csr
5. Create key, certificate & sign the CSR
openssl genrsa -aes256 -out CAPriKey.key 2024
openssl req -new -x509 -days 365 -key CAPriKey.key -out customerCA.crt

openssl x509 -req -days 365 -in myClusterCsr.csr -CA customerCA.crt -CAkey CAPriKey.key -CAcreateserial -out CustomerHsmCert.crt
6. Upload certificates to cluster
aws cloudhsmv2 initialize-cluster --cluster-id $HSM_CLUSTER_ID --signed-cert file://CustomerHsmCert.crt --trust-anchor file://customerCA.crt

7. Active the HSM cluster
wget https://s3.amazonaws.com/cloudhsmv2-software/CloudHsmClient/EL6/cloudhsm-client-latest.el6.x86_64.rpm
sudo yum install ./cloudhsm-client-latest.el6.x86_64.rpm

sudo cp customerCA.crt /opt/cloudhsm/etc/customerCA.crt
sudo /opt/cloudhsm/bin/configure -a HSM_IP

8. Login to the HSM cluster

/opt/cloudhsm/bin/cloudhsm_mgmt_util /opt/cloudhsm/etc/cloudhsm_mgmt_util.cfg

loginHSM PRECO admin password
listUsers

