I’m trying to write a template that configures the whole ecs fargate server and its code pipeline.
There is no problem in all other configurations, but the image is empty because it is right after creating the ecr in cloudformation, and the create ecs service refers to the empty image and the process does not end.
So I want to push the server image to ecr with code build and then ecs service create to work, but I don’t know how.
Could it be possible to trigger code build or code pipeline inside cloudformation?
If not, is there any way to do docker build & push?
2
Answers
Yes it can be done, I used it before to perform jobs like a database restore as part of stack creation (don’t ask). What you need:
CREATE_IN_PROGRESS
state until the endpoint is called. It’s been a while since I’ve used custom resources so don’t remember where it comes from but I think it’s found in the event that the custom resource lambda is invoked with.GET
orPOST
?) request to it, on both success and failure cases (you pass different params signifying success or failure).So the overall sequence of steps is:
CREATE_IN_PROGRESS
CREATE_IN_PROGRESS
, and the stack create/update process will wait for it, even if it takes hours.CREATE_COMPLETE
(assuming you’ve invoked the endpoint with params saying it was successful).Yes, it is possible to trigger the Fargate deployment after pushing the image rather than when the CloudFormation template is run.
The trick is to set the
DesiredCount
property ofAWS::ECS::Service
to zero:That said, you can also choose to create a repo with an initial commit that will trigger the build as soon as the template is done executing. This requires you to upload the zipped source code to an S3 bucket and configure the CodeCommit repository like so:
Note that the
some-bucket
S3 bucket needs to contain the zipped.Dockerfile
and any source code without any .git directory included.You can see my implementation of this system and the rest of the stack below:
Hope this helps!