Phillipp
Phillipp

Reputation: 392

How to configure Wordpress via AWS Fargate Container using AWS CDK

I would like to configure Wordpress via AWS Fargate in the container variant (i.e. without EC2 instances) using AWS CDK.

I have already implemented a working configuration for this purpose. However, it is currently not possible to install themes or upload files in this form, since Wordpress is located in one or more docker containers.

Here is my current cdk implementation:

AWS-CDK

export class WordpressWebsiteStack extends cdk.Stack {
    constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
        super(scope, id, props);

        // GENERAL

        const vpc = new ec2.Vpc(this, 'Vpc', {
            // 2 is minimum requirement for cluster
            maxAzs: 2,

            // only create Public Subnets in order to prevent aws to create
            // a NAT-Gateway which causes additional costs.
            // This will create 1 public subnet in each AZ.
            subnetConfiguration: [
                {
                    name: 'Public',
                    subnetType: ec2.SubnetType.PUBLIC,
                },
            ]
        });

        // DATABASE CONFIGURATION

        // Security Group used for Database
        const wordpressSg = new ec2.SecurityGroup(this, 'WordpressSG', {
            vpc: vpc,
            description: 'Wordpress SG',
        });

        // Database Cluster for wordpress database
        const dbCluster = new rds.DatabaseCluster(this, 'DBluster', {
            clusterIdentifier: 'wordpress-db-cluster',
            instances: 1,
            defaultDatabaseName: DB_NAME,
            engine: rds.DatabaseClusterEngine.AURORA, // TODO: AURORA_MYSQL?
            port: DB_PORT,
            masterUser: {
                username: DB_USER,
                password: cdk.SecretValue.plainText(DB_PASSWORD)
            },
            instanceProps: {
                instanceType: ec2.InstanceType.of(ec2.InstanceClass.BURSTABLE2, ec2.InstanceSize.SMALL),
                vpc,
                securityGroup: wordpressSg,
            }
        });

        // FARGATE CONFIGURATION

        // ECS Cluster which will be used to host the Fargate services
        const ecsCluster = new ecs.Cluster(this, 'ECSCluster', {
            vpc: vpc,
        });

        // FARGATE CONTAINER SERVICE

        const fargateService = new ecs_patterns.ApplicationLoadBalancedFargateService(this, 'WordpressFargateService', {
            cluster: ecsCluster, // Required
            desiredCount: 1, // Default is 1
            cpu: 512, // Default is 256
            memoryLimitMiB: 1024, // Default is 512

            // because we are running tasks using the Fargate launch type in a public subnet, we must choose ENABLED
            // for Auto-assign public IP when we launch the tasks.
            // This allows the tasks to have outbound network access to pull an image.
            // @see https://aws.amazon.com/premiumsupport/knowledge-center/ecs-pull-container-api-error-ecr/
            assignPublicIp: true,

            taskImageOptions: {
                image: ecs.ContainerImage.fromRegistry(wordpressRegistryName),
                environment: {
                    WORDPRESS_DB_HOST: dbCluster.clusterEndpoint.socketAddress,
                    WORDPRESS_DB_USER: DB_USER,
                    WORDPRESS_DB_PASSWORD: DB_PASSWORD,
                    WORDPRESS_DB_NAME: DB_NAME,
                },
            },
        });
        fargateService.service.connections.addSecurityGroup(wordpressSg);
        fargateService.service.connections.allowTo(wordpressSg, ec2.Port.tcp(DB_PORT));
    }
}

Perhaps someone knows how I can set up Fargate via CDK so that the individual Wordpress containers have a common volume on which the data is then located? Or maybe there is another elegant solution for this :)

Many thanks in advance :)


Found a solution 🤗

Thanks to the comments in the open GitHub-Issue and the provided Gist, I was finally able to configure a working solution.

I provided my current solution in this Gist. So feel free and just have a look at it, leave some comments and adapt it if it suites to your problem.

Upvotes: 0

Views: 1864

Answers (1)

mreferre
mreferre

Reputation: 6073

I am part of the AWS container service team and I would like to give you a bit of background re where we stand. We have recently (5/11/2020) announced the integration of Amazon ECS / AWS Fargate with Amazon EFS (Elastic File System). This is the plumbing that will allow you to achieve what you want to achieve. You can read more about the theory here and here for a practical example.

The example I linked above uses the AWS CLI simply because CloudFormation support for this feature has not been released yet (stay tuned). Once CFN support is released CDK will pick it up and, at that point, it will be able to adjust your CDK code to achieve what you want.

Upvotes: 1

Related Questions