I need that my AWS Code pipeline will run only when certain files are checked-in to my bitbucket repository in a certain branch.
I manage to achieve this with AWS Code build but as I understand it's not as simple with code pipeline.
I understand I need to attach a webhook to a lambda function which will trigger the pipeline (if needed). But I couldn't find anything about it in the management console. Searching for it only led me in circles.
It should be pretty straight-forward, Anyone has any experience with this?
So what you need to implement is the following workflow:
Create an API Gateway endpoint.
Create your CodePipeline pipeline
Create your Lambda function that will perform any checks against the code and trigger your CodePipeline
Create a method/resource in API Gateway that triggers your Lambda
Then add the API Gateway endpoint to your Bitbucket web hook.
Related
How would I make a webhook (which I'll be using from my CMS) to invoke my CodePipeline?
I've got a simple CodePipeline setup that looks like this.
Source (Github) -> AWS CodeBuild -> AWS S3
It's a Node app that builds an application based on resources from a CMS.
At the moment only committing to the Github Repository invokes CodePipeline to remake my app.
However, I'm wanting a webhook or URL trigger to invoke the CodePipeline to recreate my app when certain changes in the CMS occur.
How do I create a webhook to invoke CodePipeline?
Not sure if it is the cleanest way. If you created an API Gateway that invokes a Lambda. In the lambda you could use one of the AWS sdks to run a script. For example, If you used Python, it would be the boto3.start_build()
That is just the first though I had.
Outside of a sdk or cli, I think Pipelines only work with ECR, S3, CodeCommit, BitBucket, and Github.
I want to write a script/code/serverless script that when run adds a trigger to the specified Lambda function.
I can deploy a lambda function using a script, I have added a trigger by going to the aws console and clicking the options from there, Also added a post method to trigger the lambda.
I want to do this using a piece of code/ script that can run on was cli or anywhere.
Is it possible to do that?
Instead of using CLI or scripts you can use AWS CDK to deploy Infrastructure as Code.
For creating and deploying lambda with API Gateway check this example.
You can get yourself familiar with AWS CDK from this workshop.
There are many other resources available on internet as well.
I've been tinkering with AWS API Gateway for creating a rest api for one of my projects.
I've managed to connect it to DynamoDB Queries and to launch Lambda functions quite easily.
However one of my ideas is to be able to create CloudWatch Event Rules though it but i have not been able to set this up yet.
I want to be able to create a new scheduled task for a lambda though api gateway. The scheduled task should be a cron task.
I've been reading the documentation but i feel stuck. I know that I can solve it using another lambda but I would like to avoid it as CloudWatch Events exists as one of the connected AWS Services.
Thanks
I'm trying to trigger a Lambda function when I click on deploy in the API-Gateway console to deploy API on a stage.
I already tried with cloudwatch rule, but there is no event patterns for API-Gateway deployment.
My questions are:
Is it possible to trigger a lambda function when I click on the deploy button on API-Gateway console?
If yes, how can I do that?
Thank you
Unfortunately, there is no straight forward way for achieving this.
CloudWatch rule will not help as there is no logging generated on API deployment.
The only thing left behind a deploy action is a CloudTrail event.
The best solution I could think for this involves Amazon EventBridge which is an event bus managed service provided by AWS.
In EventBridge you can create rules that collect specific events from various AWS services within (and beyond) your AWS account.
API Gateway is not one of these services, but CloudTrail is! (For reference here is a list of the EventBridge supported services)
An API deployment in API Gateway emits an event to CloudTrail which has CreateDeployment as event name and apigateway.amazonaws.com as event source. The event payload also includes data such as the restApiId, the stage, the IAM identity details of the deploying agent and more.
Note, that there is not much documentation around CloudTrail event schemas, but the event would look something like the one listed here
Now, we need to create an EventBridge rule that captures such CloudTrail events.
This is a very good, step by step, guide on how to do this.
For your use case, you need to choose API Gateway as the service name and add CreateDeployment as a Specific Operation as shown in the screenshot below:
Once the EventBridge rule is set up then you can directly attach it as a trigger in any Lambda function. See relevant tutorial.
Downsides
The above solution cannot be applied on the individual API level. The EventBridge rule will capture the deployments of all APIs of any stage in a specific region. Additional filtering has to be implemented within the lambda logic.
This will lead to unnecessary lambda executions if the solution is scoped for anything less than all the APIs of a region. However as we're talking about API deployments, the extra lambda execution cost will be negligible.
I'm building an API using AWS API Gateway and AWS Lambda. I would like to achieve continuous delivery for this API. The path I've chosen to do it is to use CloudFormation through AWS CodePipeline. I've managed to to it for another project using Lambdas (without API Gateway), it works perfectly and it is really pleasant to use.
The issue I'm facing when deploying is that the Lambdas are properly updated but not the API definition. From what I understand, the AWS::ApiGateway::Deployment are immutable resources which means that for each deployment of the API I need to create a new AWS::ApiGateway::Deployment resource. This is not practical at all because for each of this AWS::ApiGateway::Deployment I have a new Invoke URL. This is not acceptable since I would have to either change my DNS record to the newly deployed API invoke URL or ask our API users to change the URL in their applications.
What I would like is to be able to change the API definition and the Lambdas implementations without my API users having to change anything in their applications.
How can I achieve this behavior?
I created a tutorial to highlight my issue. You can find it at: https://github.com/JonathanGailliez/aws-api-gateway-lambda-example
As per: https://forums.aws.amazon.com/thread.jspa?messageID=789869󀵭
joey-aws says:
We are currently in the process of rolling out a solution which
addresses this exact problem. In the meantime, a common workaround
would be to update something small, such as a "description" field
which could then be used to "trigger" an API Gateway deployment when
updating the CloudFormation stack.
I'll update this answer and the example repo once it's rolled out.
You could run a Cloudformation update from the command line or in the AWS console. This would change the API definitions and any lambda code without changing the unique id to access your gateway.
The other option is to put your API behind a custom domain name and then you could keep deploy a new API or stage and switch over the custom domain mapping when you are ready. The users wouldn't recognize any change.
A way to achieve that is by leveraging existing frameworks like
AWS SAM
Serverless
Claudia
I was able to achieve this by using CloudFormation template generated by troposphere and boto3 api in Python as follows:
Split the template into two parts
API definition, Method(s), IAM roles, ApiKey and Lambda (a)
Deployment, UsagePlan and UsagePlanKey (b)
Once changed Lambda code is zipped up and uploaded to S3 using boto3 api
Stack (b) is deleted
Stack (a) is updated with new resource id for the GET method connected to lambda
Stack (b) is created anew
Steps 3, 4, 5 are performed using CloudFormation boto3 api with blocking until completed.
Most importantly after all steps are done ApiKey value and stage Invoke URL remain the same, running updated Lambda code as tested with curl.
Note: it may take additional 30-60s for API to become fully functional after CloudFormation update is completed.