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

问题描述 投票:0回答:1

我想通过以下方式配置Wordpress AWS Fargate 在容器变体(即没有EC2实例)中,用 AWS CDK.

我已经为此实现了一个工作配置。然而,目前还不能以这种形式安装主题或上传文件,因为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 和所提供的 要点我终于能够配置一个有效的解决方案。

我在这里提供了我目前的解决方案 要点. 所以,请随意看看它,留下一些评论,如果它适合你的问题,就可以调整它。

wordpress docker amazon-ecs aws-fargate
1个回答
0
投票

我是AWS容器服务团队的一部分,我想给你一点背景重新我们的立场。我们最近(5112020)宣布了亚马逊ECS AWS Fargate与亚马逊EFS(弹性文件系统)的整合。这是管道,将允许你实现你想实现的目标。你可以阅读更多关于理论 此处以此为例.

我上面链接的例子使用了AWS CLI,只是因为CloudFormation对该功能的支持还没有发布(敬请期待)。一旦CFN支持发布,CDK就会接收到它,届时,它将能够调整您的CDK代码以实现您的需求。

© www.soinside.com 2019 - 2024. All rights reserved.