如何使用AWS CDK通过AWS Fargate容器配置Wordpress

时间:2020-06-09 12:45:13

标签: wordpress docker amazon-ecs aws-fargate

我想使用AWS Fargate在容器变体中(即没有EC2实例的情况下)通过AWS CDK配置Wordpress。

我已经为此目的实现了工作配置。但是,由于Wordpress位于一个或多个Docker容器中,因此目前无法以这种形式安装主题或上传文件。

这是我当前的cdk实现:

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));
    }
}

也许有人知道我如何通过Fargate来设置CDK,以便各个Wordpress容器具有一个公用的volume,然后将其放在该数据上?也许对此还有另一种优雅的解决方案:)

非常感谢:)


找到了解决方案?

多亏了公开GitHub-Issue和提供的Gist中的评论,我终于能够配置一个可行的解决方案。

我在此Gist中提供了当前的解决方案。因此,请放心看看,留下一些评论,并在适合您问题的情况下对其进行调整。

1 个答案:

答案 0 :(得分:1)

我是AWS容器服务团队的成员,我想为您提供一些背景知识。我们最近(2020年5月11日)宣布将Amazon ECS / AWS Fargate与Amazon EFS(弹性文件系统)集成。这是使您能够实现想要达到的目标的管道。您可以阅读有关理论herehere for a practical example的更多信息。

我上面链接的示例仅使用AWS CLI是因为尚未发布对此功能的CloudFormation支持(敬请期待)。释放CFN支持后,CDK会选择它,届时它将能够调整您的CDK代码以实现所需的功能。