added ecs-fargate
ADD
aws-ts-vpc-with-ecs-fargate-py/ecs-fargate-python/.gitignore
+*.pyc
+venv/

ADD
aws-ts-vpc-with-ecs-fargate-py/ecs-fargate-python/LICENSE
+MIT License
+
+Copyright (c) 2020 Tushar Shah
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.

ADD
aws-ts-vpc-with-ecs-fargate-py/ecs-fargate-python/Pulumi.yaml
+name: fargate-with-crosswalk-vpc
+runtime:
+ name: python
+ options:
+ virtualenv: venv
+description: Fargate with crosswalk vpc

ADD
aws-ts-vpc-with-ecs-fargate-py/ecs-fargate-python/README.md
+[![Deploy](https://get.pulumi.com/new/button.svg)](https://app.pulumi.com/new)
+
+# NGINX on AWS ECS Fargate using Python with a vpc built in Typescript
+
+### What Is This?
+
+This example shows authoring Infrastructure as Code in `python` for ecs fargate with a [crosswalk vpc](https://www.pulumi.com/docs/guides/crosswalk/aws/vpc/) built in `typescript`. It
+provisions a full [Amazon Elastic Container Service (ECS) "Fargate"](https://aws.amazon.com/ecs) cluster and
+related infrastructure, running a load-balanced NGINX web server accessible over the Internet on port 80.
+This example is inspired by [Docker's Getting Started Tutorial](https://docs.docker.com/get-started/).
+
+### Why would you do this?
+An example showing that you can easily infrastructure written in a different language than the one you are used to. The vpc outputs from vpc-crosswalk-ts folder are used as inputs via [StackReference](https://www.pulumi.com/docs/intro/concepts/organizing-stacks-projects/#inter-stack-dependencies)
+
+## Prerequisites
+
+* [Install Pulumi](https://www.pulumi.com/docs/get-started/install/)
+* [Configure Pulumi to Use AWS](https://www.pulumi.com/docs/intro/cloud-providers/aws/setup/) (if your AWS CLI is configured, no further changes are required)
+
+
+### Mandatory AWS Pre-REQ: AWS Console Fix for Tags:
+
+This is necessary so that the tags work properly in ecs
+[Tagging your Amazon ECS resources](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-using-tags.html)
+
+As per AWS: `You must opt in to the new Amazon Resource Name (ARN) and resource identifier (ID) formats.`
+
+This has to be done per region until AWS enables it as default across the board(April 1, 2021).
+
+AWS Console -> Elastic Container Service -> Account Settings ->
+
+BEFORE
+```
+Resource My IAM user or role account settings
+Container Instance Undefined
+Service Undefined
+Task Undefined
+```
+
+AFTER
+```
+Resource My IAM user or role account settings
+Container Instance Enabled
+Service Enabled
+Task Enabled
+```
+
+## Running the Example
+
+ 1. Initialize a new stack called: `ecs-fargate-dev` via [pulumi config](https://www.pulumi.com/docs/reference/cli/pulumi_config_set/).
+ ```
+ $ pulumi stack init ecs-fargate-dev
+ ```
+
+2. Create a Python virtualenv, activate it, and install dependencies:
+
+ This installs the dependent packages [needed](https://www.pulumi.com/docs/intro/concepts/how-pulumi-works/) for our Pulumi program.
+
+ ```bash
+ $ python3 -m venv venv
+ $ venv/bin/python -m pip install --upgrade pip setuptools wheel
+ $ venv/bin/python -m pip install -r requirements.txt
+ ```
+
+2. View the current config settings. This will be empty.
+ ```
+ $ pulumi config
+ ```
+ ```
+ KEY VALUE
+ ```
+
+4. Populate the config.
+
+ Here are aws [endpoints](https://docs.aws.amazon.com/general/latest/gr/rande.html)
+
+ Note: the mystackpath is the key. The value for it will be your `stackreference` from the vpc:
+
+ e.g.: `team-qa/crosswalk-vpc/vpc-fargate-dev`
+ ```
+ $ pulumi config set aws:region us-east-2 # must match vpc region
+ $ pulumi config set config set mystackpath shaht/crosswalk-vpc/vpc-fargate
+ ```
+
+4. View the current config settings
+ ```$ pulumi config```
+ ```
+ KEY VALUE
+ aws:region us-east-2
+ mystackpath shaht/crosswalk-vpc/vpc-fargate
+ ```
+
+5. Launch
+ ```$ pulumi up```
+
+6. Pulumi Console to view everything. Note, you will have a url that shows up that will look similar to the url below. The `shaht` will be replaced with your own org, for example if your org name is: `team-qa`:
+
+ console view that matches above code as an example:
+
+ https://app.pulumi.com/`shaht`/fargate-with-crosswalk-vpc/ecs-fargate-dev/
+
+ console view with YOUR ORG NAME:
+
+ https://app.pulumi.com/`team-qa`/fargate-with-crosswalk-vpc/ecs-fargate-dev/
+
+7. View the outputs
+
+```$ pulumi stack output```
+
+```
+ Current stack outputs (2):
+ OUTPUT VALUE
+ ECS Cluster Tags {"Name":"pulumi-fargate-ecs-cluster","application":"fargate","costcenter":"1234","crosswalk-vpc":"yes","demo":"yes","env":"dev","pulumi:Config":"Pulumi.ecs-fargate-dev.yaml","pulumi:project":"fargate-with-crosswalk-vpc","pulumi:stack":"ecs-fargate-dev","vpc_cidr":"10.0.0.0/24","vpc_name":"vpc-fargate-dev"}
+
+ Load Balancer URL pulumi-fargate-alb-7467631-1452059497.us-east-2.elb.amazonaws.com
+ ```
+
+7. Cleanup.
+ ```
+ $ pulumi destroy
+ $ pulumi rm ecs-fargate-dev
+ ```
\ No newline at end of file

ADD
aws-ts-vpc-with-ecs-fargate-py/ecs-fargate-python/__main__.py
+"""An AWS fargate with crosswalk vpc"""
+
+import pulumi
+from pulumi import export, ResourceOptions, Config, StackReference, get_stack, get_project
+import json
+import pulumi_aws as aws
+
+#https://www.pulumi.com/docs/intro/concepts/organizing-stacks-projects/#inter-stack-dependencies
+
+# read local config settings - network
+config = Config()
+
+# reading in vpc StackReference Path from local config
+mystackpath = config.get("mystackpath")
+
+# setting the StackReference
+mycrosswalkvpc = StackReference(f"{mystackpath}")
+
+# Get all network values from previously created vpc #
+pulumi_vpc = mycrosswalkvpc.get_output("pulumi_vpc_id")
+pulumi_vpc_name = mycrosswalkvpc.get_output("pulumi_vpc_name")
+pulumi_vpc_cidr = mycrosswalkvpc.get_output("pulumi_vpc_cidr")
+pulumi_vpc_id = mycrosswalkvpc.get_output("pulumi_vpc_id")
+pulumi_private_subnets = mycrosswalkvpc.get_output("pulumi_vpc_private_subnet_ids")
+pulumi_public_subnets = mycrosswalkvpc.get_output("pulumi_vpc_public_subnet_ids")
+pulumi_az_amount = mycrosswalkvpc.get_output("pulumi_vpc_az_zones")
+env_stack = get_stack()
+env_project = get_project()
+config_file = "Pulumi." + env_stack + ".yaml"
+
+# common tags
+my_tags = {"application":"fargate", "crosswalk-vpc":"yes", "demo":"yes", "costcenter":"1234", "env":"dev", "vpc_name": pulumi_vpc_name, "vpc_cidr":pulumi_vpc_cidr, "pulumi:project":env_project, "pulumi:stack":env_stack, "pulumi:Config":config_file}
+
+# Step 1.1: Create the Task Execution IAM Role https://docs.aws.amazon.com/AmazonECS/latest/userguide/ecs-cli-tutorial-fargate.html
+# IAM Role: https://www.pulumi.com/docs/reference/pkg/aws/iam/role/
+task_execution_role = aws.iam.Role(
+ "pulumi-fargate-task-execution-role",
+ assume_role_policy=json.dumps(
+ {
+ "Version": "2012-10-17",
+ "Statement":
+ [
+ {
+ "Sid": "",
+ "Effect": "Allow",
+ "Principal": {"Service": "ecs-tasks.amazonaws.com"},
+ "Action": "sts:AssumeRole"
+ }
+ ],
+ }
+ ),
+)
+
+# Step 1.2 Attach the task execution role policy: https://docs.aws.amazon.com/AmazonECS/latest/userguide/ecs-cli-tutorial-fargate.html
+# https://www.pulumi.com/docs/reference/pkg/aws/iam/rolepolicyattachment/
+task_execution_role_policy_attach = aws.iam.RolePolicyAttachment(
+ "pulumi-fargate-task-excution-policy-attach",
+ role = task_execution_role.name,
+ policy_arn="arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy"
+)
+
+# Step 3: Create a Cluster and Configure the Security Group https://docs.aws.amazon.com/AmazonECS/latest/userguide/ecs-cli-tutorial-fargate.html
+# https://www.pulumi.com/docs/reference/pkg/aws/ec2/securitygroup/
+
+# tags for security group
+sec_tags = dict(my_tags)
+sec_tags.update({"Name":"pulumi-fargate-security-group"})
+
+sgroup = aws.ec2.SecurityGroup(
+ 'pulumi-fargate-sg',
+ description='Enable HTTP access',
+ vpc_id = pulumi_vpc,
+ ingress=[
+ { 'protocol': 'tcp', 'from_port': 80, 'to_port': 80, 'cidr_blocks': ['0.0.0.0/0'] }
+ ],
+ egress = [
+ { 'protocol': '-1', 'from_port': 0, 'to_port': 0, 'cidr_blocks': ['0.0.0.0/0'] },
+ ],
+ tags = sec_tags,
+ )
+
+# tags for ecs cluster
+cluster_tags = dict(my_tags)
+cluster_tags.update({"Name":"pulumi-fargate-ecs-cluster"})
+
+# Create an ECS cluster to run a container-based service.
+# https://www.pulumi.com/docs/reference/pkg/aws/ecs/cluster/
+cluster = aws.ecs.Cluster('pulumi-app-cluster', tags = cluster_tags)
+
+
+# 3. Create a load balancer to listen for requests and route them to the container.
+ # AWS CLI install https://docs.aws.amazon.com/elasticloadbalancing/latest/application/tutorial-application-load-balancer-cli.html
+ #https://www.pulumi.com/docs/reference/pkg/aws/alb/loadbalancer/
+
+# tags for Application Load Balancer (ALB)
+alb_tags = dict(my_tags)
+alb_tags.update({"Name":"pulumi-fargate-alb"})
+
+# Created application load balancer in public subnets https://www.pulumi.com/docs/reference/pkg/aws/alb/loadbalancer/
+alb = aws.lb.LoadBalancer("pulumi-fargate-alb", subnets = pulumi_public_subnets, security_groups = [sgroup.id], tags = alb_tags)
+
+# Create target group https://www.pulumi.com/docs/reference/pkg/aws/alb/targetgroup/
+alb_target_group = aws.lb.TargetGroup(
+ "pulumi-fargate-alb-tg",
+ port=80,
+ protocol="HTTP",
+ target_type = "ip",
+ vpc_id=pulumi_vpc
+ )
+
+# Create Listener https://www.pulumi.com/docs/reference/pkg/aws/alb/listener/
+front_end_listener = aws.lb.Listener(
+ "pulumi-fargate-listener",
+ load_balancer_arn=alb.arn,
+ port = "80",
+ protocol = "HTTP",
+ default_actions=[{
+ "type": "forward",
+ "target_group_arn": alb_target_group.arn,
+ }]
+ )
+
+# tags for task definition
+task_definition_tags = dict(my_tags)
+task_definition_tags.update({"Name":"pulumi-fargate-alb"})
+
+# Task Definition https://www.pulumi.com/docs/reference/pkg/aws/ecs/taskdefinition/
+task_definition = aws.ecs.TaskDefinition(
+ "pulumi-fargate-task-definition",
+ family='fargate-task-definition',
+ cpu='256',
+ memory='512',
+ network_mode='awsvpc',
+ requires_compatibilities=['FARGATE'],
+ execution_role_arn=task_execution_role.arn,
+ tags = task_definition_tags,
+ container_definitions=json.dumps([{
+ 'name': 'pulumi-myfargate-app',
+ 'image': 'nginx',
+ 'portMappings': [{
+ 'containerPort': 80,
+ 'hostPort': 80,
+ 'protocol': 'tcp'
+ }]
+ }])
+)
+
+# ecs service tags
+service_tags = dict(my_tags)
+service_tags.update({"Name":"pulumi-fargate-service"})
+
+# ecs service https://www.pulumi.com/docs/reference/pkg/aws/ecs/service/
+service = aws.ecs.Service(
+ 'pulumi-fargate-service',
+ cluster=cluster.arn,
+ desired_count=pulumi_az_amount,
+ launch_type='FARGATE',
+ task_definition=task_definition.arn,
+ network_configuration={
+ 'assign_public_ip': 'true',
+ 'subnets': pulumi_public_subnets,
+ 'security_groups': [sgroup.id]
+ },
+ load_balancers=[{
+ 'target_group_arn': alb_target_group.arn,
+ 'container_name': 'pulumi-myfargate-app',
+ 'container_port': 80
+ }],
+ opts=ResourceOptions(depends_on=[front_end_listener]),
+ tags = service_tags,
+)
+
+export('Load Balancer URL', alb.dns_name)
+export("ECS Cluster Tags", cluster_tags)
\ No newline at end of file

ADD
aws-ts-vpc-with-ecs-fargate-py/ecs-fargate-python/requirements.txt
+pulumi>=2.0.0,<3.0.0
+pulumi-aws>=2.0.0,<3.0.0

added ecs-fargate