How can I design a cost-effective deployment architecture

17    Asked by ColemanGarvin in AWS , Asked on May 7, 2024

I am a solution architect and I am tasked with designing a cost-effective deployment architecture by using the AWS elastic beanstalk for a web-based application. How can I approach this task, considering factors such as Instance types, scaling options, storage costs, and potential optimization for minimizing elastic beanstalk overall cost while ensuring optimal performance and scalability for the applications? 

Answered by Connor Peake

In the context of AWS, here are the steps and consideration given:-

Instance types

You can choose appropriate EC2 Instance types based on your resources of the application requirements. You can use the AWS cost explorer to analyze historical usage patterns and select Instances that can balance performance and cost.

Auto Scaling

You can configure auto-scaling policies based on metrics like CPU utilization, request count, or custom cloudwatch metrics. You can set scaling triggers for adding or removing Instances dynamically, optimizing resource utilization, and minimizing costs during peak and off-peak periods.

Storage costs

You can evaluate storage options such as Amazon s3 for static content, Amazon RDS for database, and Amazon EFS for shared file storage.

Optimization

You can implement performance optimization such as caching, code optimization, and resource consolidation to reduce the number of Instances required.

Here is an example given of how you can configure auto-scaling and Instance types in an elastic beanstalk environment by using AWS CLI:-

#!/bin/bash

# CloudFormation template for Elastic Beanstalk environment

Cat << EOF> elasticbeanstalk-template.yaml

Resources:

  MyElasticBeanstalkEnvironment:

    Type: AWS::ElasticBeanstalk::Environment

    Properties:

      ApplicationName: MyApplication

      EnvironmentName: MyEnvironment

      SolutionStackName: “64bit Amazon Linux 2 v5.4.0 running Python 3.8”

      OptionSettings:

Namespace: aws:autoscaling:launchconfiguration

          OptionName: InstanceType

          Value: t3.micro

Namespace: aws:elasticbeanstalk:environment

          OptionName: EnvironmentType

          Value: LoadBalanced

Namespace: aws:autoscaling:asg

          OptionName: MinSize

          Value: ‘2’

Namespace: aws:autoscaling:asg

          OptionName: MaxSize

          Value: ‘5’

EOF

# Create CloudFormation stack

Aws cloudformation create-stack –stack-name MyElasticBeanstalkStack –template-body file://elasticbeanstalk-template.yaml –capabilities CAPABILITY_IAM

# Wait for stack creation to complete

Aws cloudformation wait stack-create-complete –stack-name MyElasticBeanstalkStack

# Configure auto-scaling policies

Aws autoscaling put-scaling-policy –auto-scaling-group-name MyAutoScalingGroup –policy-name ScaleOutPolicy –scaling-adjustment 1 –adjustment-type ChangeInCapacity

Aws autoscaling put-scaling-policy –auto-scaling-group-name MyAutoScalingGroup –policy-name ScaleInPolicy –scaling-adjustment -1 –adjustment-type ChangeInCapacity

# Create CloudWatch alarms for scaling triggers

Aws cloudwatch put-metric-alarm –alarm-name HighCPUUtilization –metric-name CPUUtilization –namespace AWS/EC2 –statistic Average –period 60 –threshold 80 –comparison-operator GreaterThanOrEqualToThreshold –evaluation-periods 2 –alarm-actions arn:aws:autoscaling:us-east-1:MyAutoScalingGroup:scale-out:force

Aws cloudwatch put-metric-alarm –alarm-name LowCPUUtilization –metric-name CPUUtilization –namespace AWS/EC2 –statistic Average –period 60 –threshold 20 –comparison-operator LessThanOrEqualToThreshold –evaluation-periods 2 –alarm-actions arn:aws:autoscaling:us-east-1:MyAutoScalingGroup:scale-in:force

# Clean up temporary files

Rm elasticbeanstalk-template.yaml

Here is the example given in java programming language:-

Import software.amazon.awssdk.regions.Region;
Import software.amazon.awssdk.services.cloudformation.CloudFormationClient;
Import software.amazon.awssdk.services.cloudformation.model.CreateStackRequest;
Import software.amazon.awssdk.services.cloudformation.model.CreateStackResponse;
Import software.amazon.awssdk.services.cloudformation.model.Tag;
Import software.amazon.awssdk.services.elasticbeanstalk.ElasticBeanstalkClient;
Import software.amazon.awssdk.services.elasticbeanstalk.model.ConfigurationOptionSetting;
Import software.amazon.awssdk.services.elasticbeanstalk.model.CreateEnvironmentRequest;
Import software.amazon.awssdk.services.elasticbeanstalk.model.CreateEnvironmentResponse;
Import java.util.ArrayList;
Import java.util.List;
Public class ElasticBeanstalkDeployer {
    Public static void main(String[] args) {
        // Initialize AWS clients
        CloudFormationClient cloudFormationClient = CloudFormationClient.builder()
                .region(Region.US_EAST_1)
                .build();
        ElasticBeanstalkClient elasticBeanstalkClient = ElasticBeanstalkClient.builder()
                .region(Region.US_EAST_1)
                .build();
        // Create CloudFormation stack for Elastic Beanstalk environment
        CreateStackResponse stackResponse = createElasticBeanstalkStack(cloudFormationClient);
        If (stackResponse != null) {
            // Extract stack name from the response
            String stackName = stackResponse.stackId();
            // Create Elastic Beanstalk environment using the CloudFormation stack
            CreateEnvironmentResponse environmentResponse = createElasticBeanstalkEnvironment(elasticBeanstalkClient, stackName);
            If (environmentResponse != null) {
                System.out.println(“Elastic Beanstalk environment created successfully!”);
            } else {
                System.out.println(“Failed to create Elastic Beanstalk environment.”);
            }
        } else {
            System.out.println(“Failed to create CloudFormation stack for Elastic Beanstalk.”);
        }
    }
    Private static CreateStackResponse createElasticBeanstalkStack(CloudFormationClient client) {
        // Define CloudFormation stack parameters
        List tags = new ArrayList<>();
        Tags.add(Tag.builder().key(“Project”).value(“MyProject”).build());
        CreateStackRequest stackRequest = CreateStackRequest.builder()
                .stackName(“MyElasticBeanstalkStack”)
                .templateBody(file://elasticbeanstalk-template.yaml)
                .tags(tags)
                .capabilities(“CAPABILITY_IAM”)
                .build();
        // Create CloudFormation stack
        Return client.createStack(stackRequest);
    }
    Private static CreateEnvironmentResponse createElasticBeanstalkEnvironment(ElasticBeanstalkClient client, String stackName) {
        // Define Elastic Beanstalk environment configuration
        List optionSettings = new ArrayList<>();
        optionSettings.add(ConfigurationOptionSetting.builder()
                .namespace(“aws:autoscaling:launchconfiguration”)
                .optionName(“InstanceType”)
                .value(“t3.micro”)
                .build());

        CreateEnvironmentRequest environmentRequest = CreateEnvironmentRequest.builder()

                .applicationName(“MyApplication”)
                .environmentName(“MyEnvironment”)
                .solutionStackName(“64bit Amazon Linux 2 v5.4.0 running Java 8”)
                .optionSettings(optionSettings)
                .build();
        // Create Elastic Beanstalk environment
        Return client.createEnvironment(environmentRequest);
    }
}


Your Answer

Interviews

Parent Categories