デフォルトVPCがない環境でPackerを実行したらエラった話


## はじめに

AWS環境でPackerを使ってゴールデンAMIを作ろうとしたら、エラったので書きます！

## 構成について

Packerは、事前に導入していることを前提とします。
また、今回作成するAMIは、Amazon Linux 2の最新を使います。

* Cloud: AWS
* Region: ap-northeast-1
* OS: Amazon Linux 2 AMI (HVM), SSD Volume Type - ami-0a2de1c3b415889d2
* Packer: 1.3.3

## Packerテンプレートの作成

以下のテンプレートを作成します。

* build.json
* variable.json

サンプルのため、**yum update**のみの単純なbuild.jsonを作成します。
AMIの指定を**amzn2-ami-hvm-*-x86_64-gp2**にすることでAMIの最新を取得することができます。
[クラスメソッドさん](https://dev.classmethod.jp/cloud/aws/packer-build-using-latest-ami/)の記事を参考にさせていただきました。ありがとうございます！

```json:build.json
{
  "builders": [{
  "type": "amazon-ebs",
  "region": "{{user `region`}}",
  "source_ami_filter": {
    "filters": {
      "name": "amzn2-ami-hvm-*-x86_64-gp2"
    },
    "owners": "137112412989",
    "most_recent": true
  },     
  "instance_type": "{{user `instance_type`}}",
  "ssh_username": "{{user `ssh_username`}}",
  "ssh_timeout": "5m",
  "ami_name": "packer-base-{{timestamp}}",
  "tags": {
    "Base_AMI_ID": "{{ .SourceAMI }}",
    "Base_AMI_NAME": "{{ .SourceAMIName }}"
    }
  }],
  "provisioners": [{
    "type": "shell",
    "inline": [
      "sudo yum -y update"
    ]
   }
 ]
}
```

次に変数用のファイルを作成します。

```bash:variable.json
{
  "ssh_username": "ec2-user",
  "region": "ap-northeast-1",
  "instance_type": "t3.micro"
}
```

## Packerを実行

Packerを実行します。

```bash
$ packer build -var-file=variable.json build.json
amazon-ebs output will be in this color.

==> amazon-ebs: Prevalidating AMI Name: amazon-linux-2-1547348873
    amazon-ebs: Found Image ID: ami-0bab560bf1ee352f5
==> amazon-ebs: Creating temporary keypair: packer_5c3aab89-2cad-f38c-0897-5c584e3becc3
==> amazon-ebs: Creating temporary security group for this instance: packer_5c3aab8a-5624-8b3d-e0d5-fff48d8f95d7
==> amazon-ebs: VPCIdNotSpecified: No default VPC for this user
==> amazon-ebs: 	status code: 400, request id: 1ca719b6-0dac-46ea-87e6-9e820aa8c9e8
==> amazon-ebs: Deleting temporary keypair...
Build 'amazon-ebs' errored: VPCIdNotSpecified: No default VPC for this user
	status code: 400, request id: 1ca719b6-0dac-46ea-87e6-9e820aa8c9e8

==> Some builds didn't complete successfully and had errors:
--> amazon-ebs: VPCIdNotSpecified: No default VPC for this user
	status code: 400, request id: 1ca719b6-0dac-46ea-87e6-9e820aa8c9e8

==> Builds finished but no artifacts were created.
```

デフォルトVPCがないよ！って怒られます。
そこで気付きました。
僕が構築しようとしているAWS環境にはデフォルトVPCがないことに。。Orz

## デフォルトVPCを作成

てことで、デフォルトVPCを作ります！
まずはAWS CLIでデフォルトVPCがないかを念のため確認します。
デフォルトVPCは、**"IsDefault": true**となっています。
**"IsDefault": false**のVPCしかない場合は、デフォルトVPCがない環境です。

```bash
$ aws ec2 --region ap-northeast-1 describe-vpcs
```

デフォルトVPCを作成します。

```bash
$ aws ec2 --region ap-northeast-1 create-default-vpc
{
    "Vpc": {
        "VpcId": "vpc-0b114178f9b840174",
        "InstanceTenancy": "default",
        "Tags": [],
        "CidrBlockAssociationSet": [
            {
                "AssociationId": "vpc-cidr-assoc-094b6605782c9f792",
                "CidrBlock": "172.31.0.0/16",
                "CidrBlockState": {
                    "State": "associated"
                }
            }
        ],
        "Ipv6CidrBlockAssociationSet": [],
        "State": "pending",
        "DhcpOptionsId": "dopt-4ab7cf2d",
        "CidrBlock": "172.31.0.0/16",
        "IsDefault": true
    }
}
```

これでデフォルトVPCが作成できました。

## 仕切り直してPackerを実行

先ほどと同様にPackerを実行します。

```bash
$ packer build -var-file=variable.json build.json
amazon-ebs output will be in this color.

==> amazon-ebs: Prevalidating AMI Name: packer-base-1547352953
    amazon-ebs: Found Image ID: ami-0bab560bf1ee352f5
==> amazon-ebs: Creating temporary keypair: packer_5c3abb79-df65-ec32-be99-a6e76d702c18
==> amazon-ebs: Creating temporary security group for this instance: packer_5c3abb7a-d5fa-19d3-f909-c4900131028a
==> amazon-ebs: Authorizing access to port 22 from 0.0.0.0/0 in the temporary security group...
==> amazon-ebs: Launching a source AWS instance...
==> amazon-ebs: Adding tags to source instance
    amazon-ebs: Adding tag: "Name": "Packer Builder"
    amazon-ebs: Instance ID: i-01ecec9982f2afc55
==> amazon-ebs: Waiting for instance (i-01ecec9982f2afc55) to become ready...
==> amazon-ebs: Using ssh communicator to connect: 3.112.38.162
==> amazon-ebs: Waiting for SSH to become available...
==> amazon-ebs: Connected to SSH!
==> amazon-ebs: Provisioning with shell script: /tmp/packer-shell353154586
    amazon-ebs: Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
    amazon-ebs: Resolving Dependencies
    amazon-ebs: --> Running transaction check
    amazon-ebs: ---> Package amazon-ssm-agent.x86_64 0:2.3.274.0-1.amzn2 will be updated
    amazon-ebs: ---> Package amazon-ssm-agent.x86_64 0:2.3.372.0-1.amzn2 will be an update
    amazon-ebs: --> Finished Dependency Resolution
    amazon-ebs:
    amazon-ebs: Dependencies Resolved
    amazon-ebs:
    amazon-ebs: ================================================================================
    amazon-ebs:  Package               Arch        Version                Repository       Size
    amazon-ebs: ================================================================================
    amazon-ebs: Updating:
    amazon-ebs:  amazon-ssm-agent      x86_64      2.3.372.0-1.amzn2      amzn2-core       15 M
    amazon-ebs:
    amazon-ebs: Transaction Summary
    amazon-ebs: ================================================================================
    amazon-ebs: Upgrade  1 Package
    amazon-ebs:
    amazon-ebs: Total download size: 15 M
    amazon-ebs: Downloading packages:
    amazon-ebs: Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
    amazon-ebs: Running transaction check
    amazon-ebs: Running transaction test
    amazon-ebs: Transaction test succeeded
    amazon-ebs: Running transaction
    amazon-ebs:   Updating   : amazon-ssm-agent-2.3.372.0-1.amzn2.x86_64                    1/2
    amazon-ebs:   Cleanup    : amazon-ssm-agent-2.3.274.0-1.amzn2.x86_64                    2/2
    amazon-ebs:   Verifying  : amazon-ssm-agent-2.3.372.0-1.amzn2.x86_64                    1/2
    amazon-ebs:   Verifying  : amazon-ssm-agent-2.3.274.0-1.amzn2.x86_64                    2/2
    amazon-ebs:
    amazon-ebs: Updated:
    amazon-ebs:   amazon-ssm-agent.x86_64 0:2.3.372.0-1.amzn2
    amazon-ebs:
    amazon-ebs: Complete!
==> amazon-ebs: Stopping the source instance...
    amazon-ebs: Stopping instance, attempt 1
==> amazon-ebs: Waiting for the instance to stop...
==> amazon-ebs: Creating unencrypted AMI packer-base-1547352953 from instance i-01ecec9982f2afc55
    amazon-ebs: AMI: ami-067336a1eb42391d0
==> amazon-ebs: Waiting for AMI to become ready...
==> amazon-ebs: Adding tags to AMI (ami-067336a1eb42391d0)...
==> amazon-ebs: Tagging snapshot: snap-0789dacf1298d6a75
==> amazon-ebs: Creating AMI tags
    amazon-ebs: Adding tag: "Base_AMI_NAME": "amzn2-ami-hvm-2.0.20190110-x86_64-gp2"
    amazon-ebs: Adding tag: "Base_AMI_ID": "ami-0bab560bf1ee352f5"
==> amazon-ebs: Creating snapshot tags
==> amazon-ebs: Terminating the source AWS instance...
==> amazon-ebs: Cleaning up any extra volumes...
==> amazon-ebs: No volumes to clean up, skipping
==> amazon-ebs: Deleting temporary security group...
==> amazon-ebs: Deleting temporary keypair...
Build 'amazon-ebs' finished.

==> Builds finished. The artifacts of successful builds are:
--> amazon-ebs: AMIs were created:
ap-northeast-1: ami-067336a1eb42391d0
```

無事にPackerを実行できましたね！
これでゴールデンAMIが作れますねヽ(*ﾟдﾟ)ノ

## まとめ

デフォルトVPCが必要であることを再認識しました。


## 参考サイト

https://stackoverflow.com/questions/47702265/eb-custom-platform-without-default-vpc-fails
https://dev.classmethod.jp/cloud/aws/packer-build-using-latest-ami/