DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Infrastructure as Code: Exploring Terraform's Dominance
  • AWS vs GCP Security: Best Practices for Protecting Infrastructure, Data, and Networks
  • Terraform Type Constraints: Best Practices for Enterprise-Scale AWS
  • The Terraform State Locking Migration You Need to Know About: Moving Beyond DynamoDB

Trending

  • Building a DevOps-Ready Internal Developer Platform: A Hands-On Guide to Golden Paths, Self-Service, and Automated Delivery Pipelines
  • Optimizing High-Volume REST APIs Using Redis Caching and Spring Boot (With Load Testing Code)
  • Feature Flag Debt: Performance Impact in Enterprise Applications
  • LLM-Powered Deep Parsing for Industrial Inventory Search
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. Autoscaling Groups With Terraform on AWS Part 2: Instance Security Group and Boot Script

Autoscaling Groups With Terraform on AWS Part 2: Instance Security Group and Boot Script

In this post, we shall add a security group to the autoscaling group and an http server to serve the requests.

By 
Emmanouil Gkatziouras user avatar
Emmanouil Gkatziouras
DZone Core CORE ·
Nov. 17, 20 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
18.5K Views

Join the DZone community and get the full member experience.

Join For Free

Previously, we followed the minimum steps required in order to spin up an autoscaling group in Terraform. In this post, we shall add a security group to the autoscaling group and an http server to serve the requests.

Using our base configuration we shall create the security group for the instances.

Go
 




x
15


 
1
resource "aws_security_group" "instance_security_group" {
2
  name = "autoscalling_security_group"
3
  ingress {
4
    from_port = 8080
5
    to_port = 8080
6
    protocol = "tcp"
7
    cidr_blocks = ["0.0.0.0/0"]
8
  }
9
  egress {
10
    from_port = 0
11
    protocol = "-1"
12
    to_port = 0
13
    cidr_blocks = ["0.0.0.0/0"]
14
  }
15
}



Our instances shall spin up a server listening at port 8080 thus the security port shall allow ingress traffic to that port. Pay attention to the egress. We shall access resources from the internet thus we want to be able to download em.

Then we will just set the security group at the launch configuration.

Go
 




xxxxxxxxxx
1


 
1
resource"aws_launch_configuration" "launch-configuration" {
2
  name = var.launch_configuration_name
3
  image_id = var.image_id
4
  instance_type = var.instance_type
5
  security_groups = ["${aws_security_group.instance_security_group.id}"]
6
}



Now it’s time to spin up a server on those instances. The aws_launch_configuration gives us the option to specify the startup script (user data on aws ec2). I shall use the Apache Ignite server and its http interface.

Go
 




xxxxxxxxxx
1
15


 
1
resource"aws_launch_configuration" "launch-configuration" {
2
  name = var.launch_configuration_name
3
  image_id = var.image_id
4
  instance_type = var.instance_type
5
  security_groups = ["${aws_security_group.instance_security_group.id}"]
6
  user_data =  <<-EOF
7
              #!/bin/bash
8
              yum install java unzip -y
9
              curl https://www-eu.apache.org/dist/ignite/2.7.6/apache-ignite-2.7.6-bin.zip -o apache-ignite.zip
10
              unzip apache-ignite.zip -d /opt/apache-ignite
11
              cd /opt/apache-ignite/apache-ignite-2.7.6-bin/
12
              cp -r libs/optional/ignite-rest-http/ libs/ignite-rest-http/
13
              ./bin/ignite.sh ./examples/config/example-cache.xml
14
              EOF
15
 
          



And now we are ready to spin up the autoscaling as shown previously.

Shell
 




xxxxxxxxxx
1


 
1
> terraform init
2
> terraform apply



We successfully added an instance security group and a bootstrap script. The next challenge is to add some load balancing and health checks.

security AWS Autoscaling Terraform (software)

Published at DZone with permission of Emmanouil Gkatziouras. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Infrastructure as Code: Exploring Terraform's Dominance
  • AWS vs GCP Security: Best Practices for Protecting Infrastructure, Data, and Networks
  • Terraform Type Constraints: Best Practices for Enterprise-Scale AWS
  • The Terraform State Locking Migration You Need to Know About: Moving Beyond DynamoDB

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook