{
	"variables": {
		"vpc": "{{env `BUILD_VPC_ID`}}",
		"subnet": "{{env `BUILD_SUBNET_ID`}}",
		"PROFILE": "{{env `AWS_PROFILE`}}",
		"STAGE": "{{env `STAGE`}}",
		"AMI_BASENAME": "{{env `AMI_BASENAME`}}"
	},
	"builders": [
		{
			"type": "amazon-ebs",
			"name": "Custom AMI builder",
			"region": "ap-northeast-1",
      "source_ami": "ami-0cc75a8978fbbc969",
			"instance_type": "t3.small",
			"ssh_username": "ec2-user",
			"ami_name": "{{user `AMI_BASENAME`}}-{{user `STAGE`}}-{{timestamp}}",
			"tags": {
				"Name": "{{user `AMI_BASENAME`}}-{{user `STAGE`}}",
				"Base_AMI_ID": "{{ .SourceAMI }}",
				"Base_AMI_NAME": "{{ .SourceAMIName }}",
                "PROJECT": "CATHERINE_FC"
			},
			"run_tags": {
				"Name": "{{user `AMI_BASENAME`}}-{{user `STAGE`}}-packer",
				"PROJECT": "CATHERINE_FC"
			},
			"run_volume_tags": {
				"Name": "{{user `AMI_BASENAME`}}-{{user `STAGE`}}-packer",
				"PROJECT": "CATHERINE_FC"
			},
			"ami_description": "Amazon Linux 2 with NodeJS",
			"vpc_id": "{{user `vpc`}}",
			"subnet_id": "{{user `subnet`}}"
		}
	],
	"provisioners": [
		{
			"type": "shell",
			"inline": [
				"sudo yum -y update",
				"sudo yum -y install unzip",
				"sudo yum -y install nano",
				"sudo yum -y install dos2unix",
				"wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash",
				". ~/.nvm/nvm.sh",
				"nvm install 12",
				"n=$(which node)",
				"n=${n%/bin/node}",
				"sudo chmod -R 755 $n/bin/*",
				"sudo cp -r $n/{bin,lib,share} /usr/local",
				"n=$(which npm)",
				"n=${n%/bin/npm}",
				"sudo chmod -R 755 $n/bin/*",
				"sudo cp -r $n/{bin,lib,share} /usr/local"
			]
		},
		{
			"type": "file",
			"source": "../platform-uploads",
			"destination": "/tmp"
		},
		{
			"type": "shell",
			"inline": [
				"sudo chown -R root:root /tmp/platform-uploads",
				"sudo dos2unix /tmp/platform-uploads/opt/web-init/web-init.sh",
				"sudo cp -rf /tmp/platform-uploads/etc/* /etc/",
				"sudo cp -rf /tmp/platform-uploads/opt/* /opt/",
				"sudo chmod 755 /opt/web-init/web-init.sh",
				"sudo systemctl enable web-init",
				"sudo rm -rf /tmp/platform-uploads"
			]
		}
	]
}
