Executing an AWS Fargate task based on a schedule (comparable to a cronjob
on Linux). Uses AWS Step Functions to control and monitor the task execution.
📚 Check out our new book Rapid Docker on AWS
- Written for DevOps engineers and web developers who want to run dockerized web applications on AWS.
- Prior knowledge of Docker and AWS is not required.
- Continuous Deployment of your Web Application and Infrastructure as Code.
Install Node.js and npm first!
npm i @cfn-modules/fargate-scheduled-task
---
AWSTemplateFormatVersion: '2010-09-09'
Description: 'cfn-modules example'
Resources:
ScheduledTask:
Type: 'AWS::CloudFormation::Stack'
Properties:
Parameters:
ClusterModule: !GetAtt 'Cluster.Outputs.StackName' # required
VpcModule: !GetAtt 'Vpc.Outputs.StackName' # required
AlertingModule: '' # optional
FileSystemModule1: '' # optional
ClientSgModule1: '' # optional
ClientSgModule2: '' # optional
ClientSgModule3: '' # optional
ManagedPolicyArns: '' # optional
AppImage: '' # optional
AppImageSecretModule '' # optional
AppEntryPoint: '' # optional
AppCommand: '' # optional
AppEnvironment1Key: '' # optional
AppEnvironment1Value: '' # optional
AppEnvironment1SecretModule: '' # optional
AppEnvironment2Key: '' # optional
AppEnvironment2Value: '' # optional
AppEnvironment2SecretModule: '' # optional
AppEnvironment3Key: '' # optional
AppEnvironment3Value: '' # optional
AppEnvironment3SecretModule: '' # optional
AppEnvironment4Key: '' # optional
AppEnvironment4Value: '' # optional
AppEnvironment5Key: '' # optional
AppEnvironment5Value: '' # optional
AppEnvironment6Key: '' # optional
AppEnvironment6Value: '' # optional
SidecarImage: '' # optional
SidecarImageSecretModule '' # optional
SidecarPort: '' # optional
SidecarEnvironment1Key: '' # optional
SidecarEnvironment1Value: '' # optional
SidecarEnvironment1SecretModule: '' # optional
SidecarEnvironment2Key: '' # optional
SidecarEnvironment2Value: '' # optional
SidecarEnvironment2SecretModule: '' # optional
SidecarEnvironment3Key: '' # optional
SidecarEnvironment3Value: '' # optional
SidecarEnvironment3SecretModule: '' # optional
Cpu: '' # optional
Memory: '' # optional
LogsRetentionInDays: '' # optional
SubnetsReach: '' # optional
ScheduleExpression: 'rate(15 minutes)' # optional
Timeout: '' # optional
CpuArchitecture: 'X86_64' # optional
OperatingSystemFamily: 'LINUX' # optional
TemplateURL: './node_modules/@cfn-modules/fargate-scheduled-task/module.yml'
none
none
Name | Description | Default | Required? | Allowed values |
---|---|---|---|---|
ClusterModule | Stack name of ecs-cluster module | yes | ||
VpcModule | Stack name of vpc module | yes | ||
AlertingModule | Stack name of alerting module | no | ||
FileSystemModule1 | Stack name of efs-file-system module mounted to /mnt/efs1 | no | ||
ClientSgModule1 | Stack name of client-sg module to mark traffic from Fargate task | no | ||
ClientSgModule2 | Stack name of client-sg module to mark traffic from Fargate task | no | ||
ClientSgModule3 | Stack name of client-sg module to mark traffic from Fargate task | no | ||
ManagedPolicyArns | Comma-delimited list of IAM managed policy ARNs to attach to the task's IAM role | no | ||
AppImage | The Docker image to use for the app container. You can use images in the Docker Hub registry or specify other repositories (repository-url/image:tag). If the repository is private, set AppImageSecretModule as well! | widdix/hello:v1 | no | |
AppImageSecretModule | Stack name of secret module which contains the repository credentials for private registry authentication | no | ||
AppEntryPoint | Optional entry point for app container. | no | ||
AppCommand | Optional command for app container. | no | ||
AppEnvironment1Key | Environment variable 1 key for app container | no | ||
AppEnvironment1Value | Environment variable 1 plain-text value for app container (if AppEnvironment1Key is set, set either AppEnvironment1Value or AppEnvironment1SecretModule) | no | ||
AppEnvironment1SecretModule | Environment variable 1 stack name of secret module for app container (if AppEnvironment1Key is set, set either AppEnvironment1Value or AppEnvironment1SecretModule) | no | ||
AppEnvironment2Key | Environment variable 2 key for app container | no | ||
AppEnvironment2Value | Environment variable 2 plain-text value for app container (if AppEnvironment2Key is set, set either AppEnvironment2Value or AppEnvironment2SecretModule) | no | ||
AppEnvironment2SecretModule | Environment variable 2 stack name of secret module for app container (if AppEnvironment2Key is set, set either AppEnvironment2Value or AppEnvironment2SecretModule) | no | ||
AppEnvironment3Key | Environment variable 3 key for app container | no | ||
AppEnvironment3Value | Environment variable 3 plain-text value for app container (if AppEnvironment3Key is set, set either AppEnvironment3Value or AppEnvironment3SecretModule) | no | ||
AppEnvironment3SecretModule | Environment variable 3 stack name of secret module for app container (if AppEnvironment3Key is set, set either AppEnvironment3Value or AppEnvironment3SecretModule) | no | ||
AppEnvironment4Key | Environment variable 4 key for app container | no | ||
AppEnvironment4Value | Environment variable 4 plain-text value for app container (if AppEnvironment4Key is set, set either AppEnvironment4Value or AppEnvironment4SecretModule) | no | ||
AppEnvironment4SecretModule | Environment variable 4 stack name of secret module for app container (if AppEnvironment4Key is set, set either AppEnvironment4Value or AppEnvironment4SecretModule) | no | ||
AppEnvironment5Key | Environment variable 5 key for app container | no | ||
AppEnvironment5Value | Environment variable 5 plain-text value for app container (if AppEnvironment5Key is set, set either AppEnvironment5Value or AppEnvironment5SecretModule) | no | ||
AppEnvironment5SecretModule | Environment variable 5 stack name of secret module for app container (if AppEnvironment5Key is set, set either AppEnvironment5Value or AppEnvironment5SecretModule) | no | ||
AppEnvironment6Key | Environment variable 6 key for app container | no | ||
AppEnvironment6Value | Environment variable 6 plain-text value for app container | no | ||
AppEnvironment7Key | Environment variable 7 key for app container | no | ||
AppEnvironment7Value | Environment variable 7 plain-text value for app container | no | ||
AppEnvironment8Key | Environment variable 8 key for app container | no | ||
AppEnvironment8Value | Environment variable 8 plain-text value for app container | no | ||
AppEnvironment9Key | Environment variable 9 key for app container | no | ||
AppEnvironment9Value | Environment variable 9 plain-text value for app container | no | ||
AppEnvironment10Key | Environment variable 10 key for app container | no | ||
AppEnvironment10Value | Environment variable 10 plain-text value for app container | no | ||
AppEnvironment11Key | Environment variable 11 key for app container | no | ||
AppEnvironment11Value | Environment variable 11 plain-text value for app container | no | ||
AppEnvironment12Key | Environment variable 12 key for app container | no | ||
AppEnvironment12Value | Environment variable 12 plain-text value for app container | no | ||
AppEnvironment13Key | Environment variable 13 key for app container | no | ||
AppEnvironment13Value | Environment variable 13 plain-text value for app container | no | ||
AppEnvironment14Key | Environment variable 14 key for app container | no | ||
AppEnvironment14Value | Environment variable 14 plain-text value for app container | no | ||
AppEnvironment15Key | Environment variable 15 key for app container | no | ||
AppEnvironment15Value | Environment variable 15 plain-text value for app container | no | ||
AppEnvironment16Key | Environment variable 16 key for app container | no | ||
AppEnvironment16Value | Environment variable 16 plain-text value for app container | no | ||
AppEnvironment17Key | Environment variable 17 key for app container | no | ||
AppEnvironment17Value | Environment variable 17 plain-text value for app container | no | ||
AppEnvironment18Key | Environment variable 18 key for app container | no | ||
AppEnvironment18Value | Environment variable 18 plain-text value for app container | no | ||
AppEnvironment19Key | Environment variable 19 key for app container | no | ||
AppEnvironment19Value | Environment variable 19 plain-text value for app container | no | ||
AppEnvironment20Key | Environment variable 20 key for app container | no | ||
AppEnvironment20Value | Environment variable 20 plain-text value for app container | no | ||
AppEnvironment21Key | Environment variable 21 key for app container | no | ||
AppEnvironment21Value | Environment variable 21 plain-text value for app container | no | ||
AppEnvironment22Key | Environment variable 22 key for app container | no | ||
AppEnvironment22Value | Environment variable 22 plain-text value for app container | no | ||
AppEnvironment23Key | Environment variable 23 key for app container | no | ||
AppEnvironment23Value | Environment variable 23 plain-text value for app container | no | ||
AppEnvironment24Key | Environment variable 24 key for app container | no | ||
AppEnvironment24Value | Environment variable 24 plain-text value for app container | no | ||
AppEnvironment25Key | Environment variable 25 key for app container | no | ||
AppEnvironment25Value | Environment variable 25 plain-text value for app container | no | ||
SidecarImage | Docker image to use for the sidecar container. You can use images in the Docker Hub registry or specify other repositories (repository-url/image:tag). If the repository is private, set SidecarImageSecretModule as well! | no | ||
SidecarImageSecretModule | Stack name of secret module which contains the repository credentials for private registry authentication | no | ||
SidecarPort | The port exposed by the sidecar container reachable from the app container on host localhost (SidecarPort != ProxyPort != AppPort) | 9000 | no | |
SidecarEnvironment1Key | Environment variable 1 key for sidecar container | no | ||
SidecarEnvironment1Value | Environment variable 1 plain-text value for sidecar container (if SidecarEnvironment1Key is set, set either SidecarEnvironment1Value or SidecarEnvironment1SecretModule) | no | ||
SidecarEnvironment1SecretModule | Environment variable 1 stack name of secret module for sidecar container (if SidecarEnvironment1Key is set, set either SidecarEnvironment1Value or SidecarEnvironment1SecretModule) | no | ||
SidecarEnvironment2Key | Environment variable 2 key for sidecar container | no | ||
SidecarEnvironment2Value | Environment variable 2 plain-text value for sidecar container (if SidecarEnvironment2Key is set, set either SidecarEnvironment2Value or SidecarEnvironment2SecretModule) | no | ||
SidecarEnvironment2SecretModule | Environment variable 2 stack name of secret module for sidecar container (if SidecarEnvironment2Key is set, set either SidecarEnvironment2Value or SidecarEnvironment2SecretModule) | no | ||
SidecarEnvironment3Key | Environment variable 3 key for sidecar container | no | ||
SidecarEnvironment3Value | Environment variable 3 plain-text value for sidecar container (if SidecarEnvironment3Key is set, set either SidecarEnvironment3Value or SidecarEnvironment3SecretModule) | no | ||
SidecarEnvironment3SecretModule | Environment variable 3 stack name of secret module for sidecar container (if SidecarEnvironment3Key is set, set either SidecarEnvironment3Value or SidecarEnvironment3SecretModule) | no | ||
SidecarEnvironment4Key | Environment variable 4 key for sidecar container | no | ||
SidecarEnvironment4Value | Environment variable 4 plain-text value for sidecar container (if SidecarEnvironment4Key is set, set either SidecarEnvironment4Value or SidecarEnvironment4SecretModule) | no | ||
SidecarEnvironment4SecretModule | Environment variable 4 stack name of secret module for sidecar container (if SidecarEnvironment4Key is set, set either SidecarEnvironment4Value or SidecarEnvironment4SecretModule) | no | ||
SidecarEnvironment5Key | Environment variable 5 key for sidecar container | no | ||
SidecarEnvironment5Value | Environment variable 5 plain-text value for sidecar container (if SidecarEnvironment5Key is set, set either SidecarEnvironment5Value or SidecarEnvironment5SecretModule) | no | ||
SidecarEnvironment5SecretModule | Environment variable 5 stack name of secret module for sidecar container (if SidecarEnvironment5Key is set, set either SidecarEnvironment5Value or SidecarEnvironment5SecretModule) | no | ||
Cpu | The minimum number of vCPUs to reserve for the container | 0.25 | no | [0.25, 0.5, 1, 2, 4] |
Memory | The amount (in GB) of memory used by the task | 0.5 | no | [0.5, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30] |
LogsRetentionInDays | Specifies the number of days you want to retain log events in the specified log group | 14 | no | [1, 3, 5, 7, 14, 30, 60, 90, 120, 150, 180, 365, 400, 545, 731, 1827, 3653] |
SubnetsReach | Should the service have direct access to the Internet or do you prefer private subnets with NAT? | Public | no | [Public, Private] |
ScheduleExpression | The schedule or rate (frequency) that determines when CloudWatch Events runs the rule (for valid values, see http://docs.aws.amazon.com/AmazonCloudWatch/latest/events/ScheduledEvents.html) | rate(1 hour) | yes | |
Timeout | The timeout for a task execution in seconds. | 600 | yes | |
CpuArchitecture | The CPU architecture of the container. | X86_64 | no | [X86_64, ARM64] |
OperatingSystemFamily | The operating system family of the container. | LINUX | no | [LINUX, WINDOWS_SERVER_2004_CORE, WINDOWS_SERVER_2016_FULL, WINDOWS_SERVER_2019_CORE, WINDOWS_SERVER_2019_FULL, WINDOWS_SERVER_2022_CORE, WINDOWS_SERVER_2022_FULL, WINDOWS_SERVER_20H2_CORE] |
To fetch Docker images from private repositories, you have to provide the repository credentials via AWS Secrets Manager. Go to AWS Secrets Manager and create a new secret of type other type) with the plaintext value:
{
"username": "DOCKERHUB_USERNAME",
"password": "DOCKERHUB_PASSWORD"
}
Use the secret module wrapper to use the secret within cfn-modules.
---
AWSTemplateFormatVersion: '2010-09-09'
Description: 'cfn-modules example'
Resources:
Secret:
Type: 'AWS::CloudFormation::Stack'
Properties:
Parameters:
Arn: 'arn:aws:secretsmanager:eu-west-1:111111111111:secret:name/of/secret' # TODO replace with your secret ARN
TemplateURL: './node_modules/@cfn-modules/secret/wrapper.yml'
The following image prameters support a secret:
Parameter | Secret |
---|---|
ProxyImage | ProxyImageSecretModule |
AppImage | AppImageSecretModule |
SidecarImage | SidecarImageSecretModule |