How to use ECS services on AWS
Overview This post will guide us through some basic understanding of ECS services in order to run dockerized applications within ECS clusters. The very first configuration file ecs.tf is pretty important to go through and try go get the idea and essence. Detailed image: Jenkins flow: ecs.tf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
$ cat ecs.tf # cluster # This is a neceassary ECS cluster which is something like bucket # with several EC2x, EC2y, ..., EC2n in it. # # resource "aws_ecs_cluster" "example-cluster" { name = "example-cluster" } # This is very similar to standard autoscaling group definition # however there is one important difference # # Two key components: # - aws_launch_configuration (something like template for EC2 instances # but notice that we use special kind of # AMI called ECS_AMIS. # # The difference is that these ECS_AMI has # special kind of service preinstalled in order # to be able a part of ECS CLUSTER) # - aws_autoscaling_group # # # resource "aws_launch_configuration" "ecs-example-launchconfig" { name_prefix = "ecs-launchconfig" image_id = "${lookup(var.ECS_AMIS, var.AWS_REGION)}" instance_type = "${var.ECS_INSTANCE_TYPE}" key_name = "${aws_key_pair.mykeypair.key_name}" iam_instance_profile = "${aws_iam_instance_profile.ecs-ec2-role.id}" security_groups = ["${aws_security_group.ecs-securitygroup.id}"] user_data = "#!/bin/bash\necho 'ECS_CLUSTER=example-cluster' > /etc/ecs/ecs.config\nstart ecs" lifecycle { create_before_destroy = true } } # # This "aws_autoscaling_group" is not much different from a standard # "aws_autoscaling_group". I would say they are basically the same. # resource "aws_autoscaling_group" "ecs-example-autoscaling" { name = "ecs-example-autoscaling" vpc_zone_identifier = ["${aws_subnet.main-public-1.id}", "${aws_subnet.main-public-2.id}"] launch_configuration = "${aws_launch_configuration.ecs-example-launchconfig.name}" min_size = 1 max_size = 1 tag { key = "Name" value = "ecs-ec2-container" propagate_at_launch = true } } |
The other important step is to […]