Skip to main content

AWS CDK package that helps deploying a fargate service with ci/cd.

Project description

AWS Ci Cd Fargate

A library that creates a full out-of-the-box solution for ECS Fargate with CI/CD pipeline.

Remarks

The project is written by Deividas Tamkus, supervised by Laimonas Sutkus and is owned by iDenfy. This is an open source library intended to be used by anyone. iDenfy aims to share its knowledge and educate market for better and more secure IT infrastructure.

Related technology

This project utilizes the following technology:

  • AWS (Amazon Web Services).
  • AWS CDK (Amazon Web Services Cloud Development Kit).
  • AWS CloudFormation.
  • AWS Loadbalancer.
  • AWS ECS (Amazon Web Services Elastic Container Service).
  • AWS Fargate (Serverless solution for ECS).
  • AWS CodePipeline.

Assumptions

This library project assumes the following:

  • You have knowledge in AWS (Amazon Web Services).
  • You have knowledge in AWS CloudFormation and AWS loadbalancing.
  • You are managing your infrastructure with AWS CDK.
  • You are writing AWS CDK templates with a python language.

Install

The project is built and uploaded to PyPi. Install it by using pip.

pip install aws-fargate-cdk

Or directly install it through source.

./build.sh -ic

Description

This package creates a Fargate service with autoscaling, balancing and two pipelines for a complete out-of-the-box hosting infrastructure.

The pipelines are as follows:

  1. ECR to ECS. This pipeline takes an image pushed to ECR and deploys it to Fargate using Blue/Green deployment. The pipeline needs to be triggered manually duo to AWS CloudWatch event bugs related to ECR.
  2. CodeCommit to ECR. This pipeline takes code pushed to the master branch of a CodeCommit repository, builds an image out of it (source code needs a Dockerfile), pushes it to ECR and automatically triggers the first pipeline, which then deploys it to ECS.

TL;DR Pushing source code with a Dockerfile to CodeCommit repository deploys it to ECS Fargate.

Examples

Create a fargate service with ci/cd:

ecs_params = EcsParams(...)
load_params = LoadBalancerParams(...)
pipeline_params = PipelineParams(...)
listener_params = LbListenerParameters(...)

EcsFargateWithCiCd(
    scope=scope,
    prefix='pre',
    vpc=vpc,
    lb_params=load_params,
    ecs_params=ecs_params,
    lb_listener_params=listener_params,
    pipeline_params=pipeline_params
)

Tutorial

  • Create a full infrastructure around ECS Fargate by using the following code below in your stack.
from aws_cdk import core, aws_ec2, aws_elasticloadbalancingv2
from aws_ci_cd_fargate.parameters.ecs_parameters import EcsParams
from aws_ci_cd_fargate.parameters.pipeline_parameters import PipelineParams
from aws_ci_cd_fargate.parameters.load_balancer_parameters import LoadBalancerParams
from aws_ci_cd_fargate.parameters.lb_listener_parameters import LbListenerParameters
from aws_ci_cd_fargate.ecs_fargate_with_ci_cd import EcsFargateWithCiCd

class MainStack(core.Stack):
    def __init__(self, scope: core.App) -> None:
        super().__init__(
            scope=scope,
            id='MyCoolStack'
        )

        # Create your own vpc or use an existing one.
        vpc = aws_ec2.Vpc(...)

        # Create a security group for your ECS Fargate instances.
        sg = aws_ec2.SecurityGroup(...)

        # Create a loadbalancer.
        loadbalancer = aws_elasticloadbalancingv2.ApplicationLoadBalancer(...)
        production_listener = aws_elasticloadbalancingv2.ApplicationListener(self, 'Prod', load_balancer=loadbalancer)
        deployments_listener = aws_elasticloadbalancingv2.ApplicationListener(self, 'Test', load_balancer=loadbalancer)

        ecs_params = EcsParams('FargateEcsContainer', 256, 512, 80, {}, [sg], vpc.private_subnets)
        load_params = LoadBalancerParams()
        pipeline_params = PipelineParams()
        listener_params = LbListenerParameters(
            production_listener=production_listener,
            deployment_listener=deployments_listener,
            rule_priority=100,
            rule_condition=aws_elasticloadbalancingv2.CfnListenerRule.RuleConditionProperty(
                    field='path-pattern',
                    path_pattern_config=aws_elasticloadbalancingv2.CfnListenerRule.PathPatternConfigProperty(
                        values=['/*']
                    )
                )
        )

        self.ecs_infrastructure = EcsFargateWithCiCd(
            scope=self,
            prefix='MyCool',
            vpc=vpc,
            lb_params=load_params,
            ecs_params=ecs_params,
            lb_listener_params=listener_params,
            pipeline_params=pipeline_params
        )

        # Access CodeCommit-To-Ecr pipeline.
        _ = self.ecs_infrastructure.pipeline.commit_to_ecr

        # Access Ecr-To-Ecs pipeline.
        _ = self.ecs_infrastructure.pipeline.ecr_to_ecs
  • Provision you infrastructure with CloudFormation by calling cdk deploy.

  • Create a Dockerfile as simple as:

FROM nginx
  • After you provision your infrastructure, go to AWS CodeCommit in your AWS Console.

  • Find a newly created git repository.

  • Commit the Dockerfile to the newly created repository to trigger a pipeline.

(A tutorial on pushing code to remote repositories: AWS Tutorial).

(A tutorial on setting up git ssh with aws git repositories: AWS Tutorial)

Release history

7.3.0

Add md files.

7.2.0

AWS CDK dependency update 1.60.0 - 2.0.0.

7.1.0

Force dependency update 1.44.0 AWS CDK.

7.0.0

Do not allow to specify ecs port. Default to 80. This is AWS limitation.

6.0.0

Instead of taking path config as a parameter, take the whole rule config.

5.0.1

Use empty ecs repository dependency.

5.0.0

Project rename to a better and more explanatory name.

4.0.9

Dependency update.

4.0.8

Dependency update. Fix ecs service name key.

4.0.7

Dependency update.

4.0.6

Use newest cdk with newest breaking changes.

4.0.5

Add dependency which creates ecs service. CloudFormation itself has too many bugs.

4.0.2

Destroy ecr on delete.

4.0.0

Build environment variable fix.

4.0.0

Add pipeline parameters and fix some major pipeline bugs.

3.1.0

Create custom resource to create ecs service. With deployment controller CODE_DEPLOY you can not do CF updates.

3.0.2

Minor bug fix.

3.0.1

Readme fix.

3.0.0

Full project refactor. Accept loadbalancer's listeners for production and test traffic instead of creting a loadbalancer here. This way we can reuse an existing loadbalancer.

2.0.0

Remove pipeline parameters as the artifacts bucket should be created automatically within this stack.

1.0.0

Initial project.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aws_ci_cd_fargate-7.3.0.tar.gz (16.1 kB view details)

Uploaded Source

Built Distribution

aws_ci_cd_fargate-7.3.0-py3-none-any.whl (33.8 kB view details)

Uploaded Python 3

File details

Details for the file aws_ci_cd_fargate-7.3.0.tar.gz.

File metadata

  • Download URL: aws_ci_cd_fargate-7.3.0.tar.gz
  • Upload date:
  • Size: 16.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.37.0 CPython/2.7.17

File hashes

Hashes for aws_ci_cd_fargate-7.3.0.tar.gz
Algorithm Hash digest
SHA256 a618b21cfe1fd06e48d6ca675530d4b7a95a849029a487557f1af25c1df08e24
MD5 9452782b977a0aa1d58e0a99938273b5
BLAKE2b-256 33c19b082d38bc804255bcf0dac7ca1107485630f7a8fcf4e9ae1b3a025b65f9

See more details on using hashes here.

File details

Details for the file aws_ci_cd_fargate-7.3.0-py3-none-any.whl.

File metadata

  • Download URL: aws_ci_cd_fargate-7.3.0-py3-none-any.whl
  • Upload date:
  • Size: 33.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.37.0 CPython/2.7.17

File hashes

Hashes for aws_ci_cd_fargate-7.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6ed25b49a930c4792f8e2c9d7e7c54fb504824fff0e7ce299bb7e11b2bbb5528
MD5 6fd21098cd9161837c40fe95676d8877
BLAKE2b-256 78d6a09d55271c43156b2f7ce68f1e530926d7e328e87532ffb2e77690ef5cc6

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page