Learn How to Create an Amazon ECS Windows Task for the EC2 Launch Type

Posted by taufik

August 20, 2024

Secret Manager and Security Hub 6

Get started with Amazon ECS using the EC2 launch type by registering a task definition, creating a cluster, and creating a service in the console. Complete the following steps to get started with Amazon ECS using the EC2 launch type. 

For more information, see Amazon ECS task execution IAM role. 

Prerequisites 

Before you begin, complete the steps in Set up to use Amazon ECS and that your AWS user has the permissions specified in the AdministratorAccess IAM policy example. 

The console attempts to automatically create the task execution IAM role, which is required for Fargate tasks. To ensure that the console is able to create this IAM role, one of the following must be true: 

  • A user with administrator access has manually created the task execution role so that it is available on the account to be used. For more information, see Amazon ECS task execution IAM role. 

Step 1: Create a cluster 

An Amazon ECS cluster is a logical grouping of tasks, services, and container instances. 

The following steps walk you through creating a cluster with one Amazon EC2 instance registered to it which will enable us to run a task on it. If a specific field is not mentioned, leave the default console values. 

To create a new cluster (Amazon ECS console) 

Before you begin, assign the appropriate IAM permission. For more information, see the section called “Amazon ECS cluster examples”. 

1. Open the console at https://console.aws.amazon.com/ecs/v2. 

2. From the navigation bar, select the Region to use. 

3. In the navigation pane, choose Clusters. 

4. On the Clusters page, choose Create cluster. 

5. Under Cluster configuration, for Cluster name, enter a unique name.
The name can contain up to 255 letters (uppercase and lowercase), numbers, and hyphens. 

6. (Optional) To change the VPC and subnets where your tasks and services launch, under Networking, perform any of the following operations: 

  • To remove a subnet, under Subnets, choose X for each subnet that you want to remove. 
  • To change to a VPC other than the default VPC, under VPC, choose an existing VPC, and then under Subnets, select each subnet.

7. To add Amazon EC2 instances to your cluster, expand Infrastructure, and then select Amazon EC2 instances. Next, configure the Auto Scaling group which acts as the capacity provider: 

a. To using an existing Auto Scaling group, from Auto Scaling group (ASG), select the group. 

b. To create a Auto Scaling group, from Auto Scaling group (ASG), select Create new group, and then provide the following details about the group:

  • For Operating system/Architecture, choose the Amazon ECS-optimized AMI for the Auto Scaling group instances. 
  • For EC2 instance type, choose the instance type for your workloads. For more information about the different instance types, see Amazon EC2 Instances.
    Managed scaling works best if your Auto Scaling group uses the same or similar instance types. 
  • For SSH key pair, choose the pair that proves your identity when you connect to the instance. 
  • For Capacity, enter the minimum number and the maximum number of instances to launch in the Auto Scaling group. Amazon EC2 instances incur costs while they exist in your AWS resources. For more information, see Amazon EC2 Pricing.

8. (Optional) To turn on Container Insights, expand Monitoring, and then turn on Use Container Insights. 

7. (Optional) To manage the cluster tags, expand Tags, and then perform one of the following operations:
[Add a tag] Choose Add tag and do the following: 

  • For Key, enter the key name. 
  • For Value, enter the key value.

[Remove a tag] Choose Remove to the right of the tag’s Key and Value.

 

10. Choose Create. 

 

Step 2: Register a task definition 

To register the sample task definition with the AWS Management Console 

1. In the navigation pane, choose Task Definitions. 

2. Choose Create new task definition, Create new task definition with JSON. 

3. Copy and paste the following example task definition into the box, and then choose Save. 

 

{ 

    “containerDefinitions”: [

        {

            “command”: [“New-Item -Path C:\\inetpub\\wwwroot\\index.html -Type file -Value ‘<html> <head> <title>Amazon ECS Sample App</title> <style>body {margin-top: 40px; background-color: #333;} </style> </head><body> <div style=color:white;text-align:center> <h1>Amazon ECS Sample App</h1> <h2>Congratulations!</h2> <p>Your application is now running on a container in Amazon ECS.</p>’; C:\\ServiceMonitor.exe w3svc”],

            “entryPoint”: [

                “powershell”,

                “-Command”

            ],

            “essential”: true,

            “cpu”: 2048,

            “memory”: 4096,

            “image”: “mcr.microsoft.com/windows/servercore/iis:windowsservercore-ltsc2019”,

            “name”: “sample_windows_app”,

            “portMappings”: [

                {

                    “hostPort”: 443,

                    “containerPort”: 80,

                    “protocol”: “tcp”

                }

            ]

        }

    ],

    “memory”: “4096”,

    “cpu”: “2048”,

    “family”: “windows-simple-iis-2019-core”,

    “executionRoleArn”: “arn:aws:iam::012345678910:role/ecsTaskExecutionRole”,

    “runtimePlatform”: {“operatingSystemFamily”: “WINDOWS_SERVER_2019_CORE”},

    “requiresCompatibilities”: [“EC2”]

} 

 

4. Verify your information and choose Create. 

 

Step 3: Create a Service 

An Amazon ECS service helps you to run and maintain a specified number of instances of a task definition simultaneously in an Amazon ECS cluster. If any of your tasks should fail or stop for any reason, the Amazon ECS service scheduler launches another instance of your task definition to replace it in order to maintain the desired number of tasks in the service. For more information on services, see Amazon ECS services. 

To create a service 

1. In the navigation pane, choose Clusters. 

2. Select the cluster you created in Step 1: Create a cluster. 

3. On the Services tab, choose Create. 

4. In the Environment section, do the following: 

  • For Compute options, choose Launch type. 
  • For Launch type, select EC2.

5. In the Deployment configuration section, do the following: 

6. Review the options and choose Create. 

7. Choose View service to review your service. 

 

Step 4: View your Service 

The service is a web-based application so you can view its containers with a web browser. 

1. Open the console at https://console.aws.amazon.com/ecs/v2. 

2. In the navigation pane, choose Clusters. 

3. Choose the cluster where you ran the service. 

4. In the Services tab, under Service name, choose the service you created in Step 3: Create a Service. 

5. Choose the Tasks tab, and then choose the task in your service. 

6. On the task page, in the Configuration section, under Public IP, choose Open address. The screenshot below is the expected output. 

 

Step 5: Clean Up 

When you are finished using an Amazon ECS cluster, you should clean up the resources associated with it to avoid incurring charges for resources that you are not using. 

Some Amazon ECS resources, such as tasks, services, clusters, and container instances, are cleaned up using the Amazon ECS console. Other resources, such as Amazon EC2 instances, Elastic Load Balancing load balancers, and Auto Scaling groups, must be cleaned up manually in the Amazon EC2 console or by deleting the AWS CloudFormation stack that created them. 

1. In the navigation pane, choose Clusters. 

2. On the Clusters page, select the cluster cluster you created for this tutorial. 

3. Choose the Services tab. 

4. Select the service, and then choose Delete. 

5. At the confirmation prompt, enter delete and then choose Delete. 

6. Wait until the service is deleted. 

7. Choose Delete Cluster. At the confirmation prompt, enter delete cluster-name, and then choose Delete. Deleting the cluster cleans up the associated resources created with it, including Auto Scaling groups, VPCs, or load balancers. 

 

 

Credit to: AWS Documentation 

whatsapp icon.png
Start a Conversation

Privacy & Policy

PT Central Data Technology (“CDT” or “us”) is strongly committed to ensuring that your privacy is protected as utmost importance to us. https://centraldatatech.com/ , we shall govern your use of this website, including all pages within this website (collectively referred to herein below as this “Website”), we want to contribute to providing a safe and secure environment for visitors.

The following are terms of privacy policy (“Privacy Policy”) between you (“you” or “your”) and CDT. By accessing the website, you acknowledge that you have read, understood and agree to be bound by this Privacy Policy

Use of The Subscription Service by CDT and Our Customers

When you request information from CDT and supply information that personally identifies you or allows us to contact you, you agree to disclose that information with us. CDT may disclose such information for marketing, promotional and activity only for the purpose of CDT and the Website.

Collecting Information

You are free to explore the Website without providing any personal information about yourself. When you visit the Website or register for the subscription service, we provide some navigational information for you to fill out your personal information to access some content we offered.

CDT may collect your personal data such as your name, email address, company name, phone number and other information about yourself or your business. We are collecting your data in some ways, online and offline. CDT collects your data online using features of social media, email marketing, website, and cookies technology. We may collect your data offline in events like conference, gathering, workshop, etc. However, we will not use or disclose those informations with third party or send unsolicited email to any of the addresses we collect, without your express permission. We ensure that your personal identities will only be used in accordance with this Privacy Policy.

How CDT Use the Collected Information

CDT use the information that is collected only in compliance with this privacy policy. Customers who subscribe to our subscription services are obligated through our agreements with them to comply with this Privacy Policy.

In addition to the uses of your information, we may use your personal information to:

  • Improve your browsing experience by personalizing the websites and to improve the subscription services.
  • Send information about CDT.
  • Promote our services to you and share promotional and informational content with you in accordance with your communication preferences.
  • Send information to you regarding changes to our customers’ terms of service, Privacy Policy (including the cookie policy), or other legal agreements

Cookies Technology

Cookies are small pieces of data that the site transfers to the user’s computer hard drive when the user visits the website. Cookies can record your preferences when visiting a particular site and give the advantage of identifying the interest of our visitor for statistical analysis of our site. This information can enable us to improve the content, modifying and making our site more user friendly.

Cookies were used for some reasons such as technical reasons for our website to operate. Cookies also enable us to track and target the interest of our users to enhance the experience of our website and subscription service. This data is used to deliver customized content and promotions within the Helios to customers who have an interest on particular subjects.

You have the right to decide whether to accept or refuse cookies. You can edit your cookies preferences on browser setup. If you choose to refuse the cookies, you may still use our website though your access to some functionality and areas of our website may be restricted.

This Website may also display advertisements from third parties containing links to other websites of interest. Once you have used these links to leave our site, please note that we do not have any control over the website. CDT cannot be responsible for the protection and privacy of any information that you provide while visiting such websites and this Privacy Policy does not govern such websites.

Control Your Personal Data

CDT give control to you to manage your personal data. You can request access, correction, updates or deletion of your personal information. You may unsubscribe from our marketing activity by clicking unsubscribe us from the bottom of our email or contacting us directly to remove you from our subscription list.

We will keep your personal information accurate, and we allow you to correct or change your personal identifiable information through marketing@centraldatatech.com