kotamat
だけkotamats
最近はあんまり主催できてない…
手伝ってくれる人いたらぜひ!
エージェント
転職者
求人企業
#!/bin/bash
if [[ "$1" == *artisan* ]]; then
set -- php "$@"
else
set -- php-fpm "$@"
fi
exec "$@"
# composer installとか
# apt-getとか
RUN chmod +x /app/docker/php-fpm/entrypoint.sh
ENTRYPOINT ["/app/docker/php-fpm/entrypoint.sh"]
version: '3.1'
services:
php-fpm:
build:
context: .
dockerfile: docker/php-fpm/Dockerfile
env_file:
- .env
worker:
build:
context: .
dockerfile: docker/php-fpm/Dockerfile
env_file:
- .env
command:
- "artisan"
- "queue:work"
- "sqs"
- "--sleep=3"
- "--tries=3"
resource "aws_ecs_task_definition" "main" {
container_definitions = jsonencode(
[
// worker
{
image = "baseimage:latest"
essential = false
name = "laravel-worker"
command = [
"artisan",
"queue:work",
"sqs",
"--sleep=3",
"--tries=3"
],
},
// api
{
image = "baseimage:latest"
essential = true
name = "laravel-api"
}
]
)
}
resource "aws_iam_role" "activation_create" {
name = "${var.service_name}-for-create-activation"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Effect = "Allow"
Principal = {
Service = "ssm.amazonaws.com"
}
Action = "sts:AssumeRole"
}
]
})
}
resource "aws_iam_role_policy_attachment" "activation_create" {
policy_arn = "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"
role = aws_iam_role.activation_create.name
}
resource "aws_iam_policy" "activate_code" {
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Effect = "Allow"
Action = [
"iam:PassRole",
"ssm:CreateActivation"
],
Resource = [
aws_iam_role.activation_create.arn
]
}
]
})
}
resource "aws_iam_role_policy_attachment" "use_activation" {
policy_arn = aws_iam_policy.activate_code.arn
role = aws_iam_role.task_role_with_ssm.name
}
if [ "$SSM_ACTIVATE" = "true" ]; then
# task実行の場合なので、厳密にエラーを補足する
set -e
ACTIVATE_PARAMETERS=$(aws ssm create-activation \
--default-instance-name "$APP_NAME-ssm" \
--description "$APP_NAME-ssm" \
--iam-role "$APP_NAME-for-create-activation" \
--registration-limit 5)
export ACTIVATE_CODE=$(echo $ACTIVATE_PARAMETERS | jq -r .ActivationCode)
export ACTIVATE_ID=$(echo $ACTIVATE_PARAMETERS | jq -r .ActivationId)
amazon-ssm-agent -register -code "${ACTIVATE_CODE}" \
-id "${ACTIVATE_ID}" -region "${AWS_DEFAULT_REGION}" -y
amazon-ssm-agent
fi
<?php
return [
'channels' => [
'ecs' => [
'driver' => 'stack',
'channels' => ['stderr', 'stdout'],
],
'stdout' => [
'driver' => 'monolog',
'level' => 'info',
'handler' => StreamHandler::class,
'formatter' => env('LOG_STDOUT_FORMATTER'),
'formatter_with' => [
'dateFormat' => '%Y-%m-%d %H:%M:%S'
],
'with' => [
'stream' => 'php://stdout',
],
],
'stderr' => [
'driver' => 'monolog',
'level' => 'critical',
'handler' => StreamHandler::class,
'formatter' => env('LOG_STDERR_FORMATTER'),
'formatter_with' => [
'dateFormat' => '%Y-%m-%d %H:%M:%S'
],
'with' => [
'stream' => 'php://stderr',
],
],
[
{
// ... 諸々のコンテナの設定
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group": "${awslog_group}",
"awslogs-region": "${region}",
"awslogs-datetime-format": "%Y-%m-%d %H:%M:%S",
"awslogs-stream-prefix": "ecs"
}
}
},
]
jobs:
job:
runs-on: ubuntu-latest
steps:
# ...
- name: Change Task Definition
id: render-td
uses: aws-actions/amazon-ecs-render-task-definition@v1
with:
task-definition: ${{ steps.fetch-td.outputs.task-definition }}
container-name: my-container-name
image: ${{ steps.built-image.outputs.image }}
- name: Deploy to Amazon ECS service
uses: aws-actions/amazon-ecs-deploy-task-definition@v1
with:
task-definition: ${{ steps.render-td.outputs.task-definition }}
service: ecs-service
cluster: ecs-cluster
jobs:
job:
runs-on: ubuntu-latest
steps:
# ...
- name: Change Task Definition for api
id: render-td-api
uses: aws-actions/amazon-ecs-render-task-definition@v1
with:
task-definition: ${{ steps.fetch-td.outputs.task-definition }}
container-name: my-container-name
image: ${{ steps.built-image.outputs.image }}
- name: Change Task Definition for worker
id: render-td-worker
uses: aws-actions/amazon-ecs-render-task-definition@v1
with:
task-definition: ${{ steps.render-td-api.outputs.task-definition }}
container-name: my-container-name
image: ${{ steps.built-image.outputs.image }}
- name: Deploy to Amazon ECS service
uses: aws-actions/amazon-ecs-deploy-task-definition@v1
with:
task-definition: ${{ steps.render-td-worker.outputs.task-definition }}
service: ecs-service
cluster: ecs-cluster
jobs:
job:
runs-on: ubuntu-latest
steps:
# ...
- name: Change Task Definition for api
id: render-td-api
uses: aws-actions/amazon-ecs-render-task-definition@v1
with:
task-definition: ${{ steps.fetch-td.outputs.task-definition }}
container-name: my-container-name
image: ${{ steps.built-image.outputs.image }}
- name: Change Task Definition for worker
id: render-td-worker
uses: aws-actions/amazon-ecs-render-task-definition@v1
with:
task-definition: ${{ steps.render-td-api.outputs.task-definition }}
container-name: my-container-name
image: ${{ steps.built-image.outputs.image }}
- name: Deploy to Amazon ECS service
uses: aws-actions/amazon-ecs-deploy-task-definition@v1
with:
task-definition: ${{ steps.render-td-worker.outputs.task-definition }}
service: ecs-service
cluster: ecs-cluster
jobs:
job:
runs-on: ubuntu-latest
steps:
# ...
- name: Deploy to Amazon ECS service
uses: aws-actions/amazon-ecs-deploy-task-definition@v1
with:
task-definition: ${{ steps.render-td-worker.outputs.task-definition }}
service: ecs-service
cluster: ecs-cluster
wait-for-service-stability: true #←これ!
// 内部実装
const waitForService = core.getInput('wait-for-service-stability', { required: false });
// Wait for service stability
if (waitForService && waitForService.toLowerCase() === 'true') {
core.debug(`Waiting for the service to become stable.
Will wait for ${waitForMinutes} minutes`);
const maxAttempts = (waitForMinutes * 60) /
WAIT_DEFAULT_DELAY_SEC;
await ecs.waitFor('servicesStable', {
services: [service],
cluster: clusterName,
$waiter: {
delay: WAIT_DEFAULT_DELAY_SEC,
maxAttempts: maxAttempts
}
}).promise();
resource "aws_cloudwatch_event_rule" "main" {
name = local.base_name
event_pattern = jsonencode({
"source" : [
"aws.ecs"
],
"detail-type" : [
"ECS Task State Change"
],
"detail" : {
"clusterArn" : [
var.cluster_arn
]
}
})
}
文章でみたい方はこちら
https://kotamat.com/post/laravel-on-ecs/
環境変数の管理、マイグレーションとかも言及してます