$ brew install awscli packer terraform
$ aws configure # setup your own (region: ap-northeast-1)
- Create Deployment S3 Bucket by manually
- e.g.
aws s3 mb s3://sampledeploy
- this s3 bucket is used for CodeDeploy or LaunchConfiguration for EC2 Instance (put/pull zip file)
- e.g.
- Create AMI with Packer
- Run Terraform
- Deploy Java Application
- Continuous Deploy, just do
deploy.sh
- Continuous Deploy, just do
packer
terraform
javaapp
Create AMI
- require
AWS_ACCESS_KEY
&AWS_SECRET_KEY
- who has credentials
- this AMI includes followings
- CodeDeploy agent
user-data.sh
which run when instance is up
$ cd packer
$ packer build sampleapp.json
aws provisioner for ap-northeast-1
. this requires AMI
which is build by above Packer
. (ami_id
is automatically selected with aws api)
$ cd terraform
$ terraform init
example settings (this settings is for terraform managed state)
$ terraform init
The name of the S3 bucket
Enter a value: terraformsample
The path to the state file inside the bucket
Enter a value: sampleapp
The region of the S3 bucket.
Enter a value: ap-northeast-1
$ terraform plan
$ terraform apply
- start point is
terraform/module_sample.tf
.ami_id
is fetched bydata resource
(seeterraform/modules/sample/data.f
)
If you want to ssh instances, apply following diff. (bastion server is not ready)
diff --git a/terraform/modules/sample/aws_launch_configuration.tf b/terraform/modules/sample/aws_launch_configuration.tf
index b6720ea..6146565 100644
--- a/terraform/modules/sample/aws_launch_configuration.tf
+++ b/terraform/modules/sample/aws_launch_configuration.tf
@@ -7,4 +7,5 @@ resource "aws_launch_configuration" "sample" {
name = "sample"
security_groups = ["${aws_security_group.sample_web.id}"]
user_data = "${var.aws_launch_configuration_sample_user_data}"
+ key_name = "YOUR_KEYPAIR_KEY_NAME"
}
javaapp/deploy/appspec.yml
- CodeDeploy scripts
- this is used for continuous deployment
$ cd javaapp
$ ./gradlew clean bootRun
- endpoints
$ curl localhost:8080 # 200 OK
$ curl localhost:8080/check/status # 200 OK
$ curl -XPOST localhost:8080/check/off # 200 OK
$ curl localhost:8080/check/status # 503 SERVICE UNAVAILABLE
deploy.sh
- create executable jar
- aws deploy
- zip following files (
deploy
directory)- executable jar
- application initialize script
- appspec.yml (for CodeDeploy)
- upload zip into s3
- zip following files (
- run create-deployment
generally this script is executed by jenkins or somewhere