• Что бы вступить в ряды "Принятый кодер" Вам нужно:
    Написать 10 полезных сообщений или тем и Получить 10 симпатий.
    Для того кто не хочет терять время,может пожертвовать средства для поддержки сервеса, и вступить в ряды VIP на месяц, дополнительная информация в лс.

  • Пользаватели которые будут спамить, уходят в бан без предупреждения. Спам сообщения определяется администрацией и модератором.

  • Гость, Что бы Вы хотели увидеть на нашем Форуме? Изложить свои идеи и пожелания по улучшению форума Вы можете поделиться с нами здесь. ----> Перейдите сюда
  • Все пользователи не прошедшие проверку электронной почты будут заблокированы. Все вопросы с разблокировкой обращайтесь по адресу электронной почте : info@guardianelinks.com . Не пришло сообщение о проверке или о сбросе также сообщите нам.

AWS Serverless: Build a Custom IAM Policy for Lambda CI/CD with SAM and GitHub – Part 2

Lomanu4 Оффлайн

Lomanu4

Команда форума
Администратор
Регистрация
1 Мар 2015
Сообщения
1,481
Баллы
155
In my previous article, I explained how to create a CICD pipeline to build and deploy a simple lambda function integrated with API Gateway using AWS SAM and GitHub.

Please make sure you've reviewed this article on this CI/CD topic (link below). I won’t repeat that content here, but understanding it is essential for following along with the custom policy setup in this article.


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



While building this solution, I temporarily assigned broad permissions such as full access to services like S3, CloudFormation, Lambda and API Gateway.

Assigning these full access permission is against the principle of least privilege that means grant only the specific permissions required.

In this article, I will focus on replacing these broad permissions with a custom fine grained IAM policy that only grants the permissions required by this pipeline.

This approach aligns with AWS security best practices and the principle of least privilege helping reduce security risks and improve overall security posture of the AWS environment.

Let's look at the architecture diagram!


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



Why Use a Custom Policy?

Using custom policies for CICD pipelines offers several advantages for enterprises. Principle of least privilege is an important factor for overall security posture. By implementing the principle of least privilege, you can significantly improve your security posture and operational efficiency.

Custom policies can help in several areas including:

Limit potential impact if credentials are ever compromised

  • Example: Restrict S3 access to only the specific bucket used for SAM deployment artifacts rather than allowing wildcard access to all buckets.
  • Example: Limit CloudFormation permissions to only the named stack used for deploying the Lambda and API resources.

Awareness of what permissions their automation requires

  • Example: Allow updates to specific Lambda functions, but restrict permission to create or delete unrelated AWS resources like DynamoDB or IAM roles.
  • Example: Permit API Gateway stage deployments only to defined stages like dev, test or permission to specific methods like Get and Post

Prevent unintended resource creation or deletion

  • Example: Deny s3:DeleteBucket and cloudformation:DeleteStack to avoid accidental loss of critical deployment artifacts or stacks.

Support document and demonstrate security control required for compliance

  • Example: Create IAM policies that clearly define allowed actions (like lambda:UpdateFunctionCode, apigateway:POST, cloudformation:DescribeStacks) and map directly to each pipeline step.
  • Example: Provide audit-ready IAM documentation showing that the CI/CD pipeline operates with least privilege.

Review Custom Policy

Below is an example of a custom IAM policy tailored for a CI/CD pipeline that deploys a Lambda function and API Gateway using AWS SAM. This pipeline is triggered by a code push to a GitHub repository and uses AWS CodePipeline and CodeBuild to build and deploy the serverless application.

The policy grants only the necessary permissions to interact with Lambda, API Gateway, S3, and CloudFormation, following the principle of least privilege.


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



Once this policy is drafted, attach it to the role for the code build/code pipeline.

Push a code change to Git Repo

Let’s make a change to the Lambda function and commit the updated code to the Git repository.

Once the changes are pushed, this will automatically trigger the AWS Code Pipeline.


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



After the pipeline starts, we’ll review the build and deployment stages to confirm the updated function is successfully redeployed.

You can see that the pipeline automatically triggered the Source and Build stages, both of which completed successfully!


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



Validate by Invoking the deployed function and API

Since the pipeline has successfully completed, the updated Lambda function and API have been deployed. Let's validate again using Postman.


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



You should now see the updated message returned by the Lambda function through the API endpoint, confirming that the changes were successfully deployed.

Review S3 bucket for Code artifact

You can also review the S3 bucket where AWS Code Pipeline stores the build artifacts generated during the pipeline execution.


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



Review Cloud Watch log

The Lambda function is integrated with CloudWatch, and a dedicated log group is automatically created.

You can view the logs by navigating to the function’s log group in the CloudWatch Logs section of the AWS Console.


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



Cleanup - Delete resources

Once you’ve completed this exercise, make sure to delete all the resources created to avoid unnecessary charges that could impact your overall cloud cost and budget.

In particular, remember to delete the following:

  • The AWS Code Pipeline
  • The CodeBuild project
  • Any S3 buckets used for artifacts (if not reused elsewhere)
  • The Lambda function, API Gateway, and associated CloudWatch log groups
  • IAM roles created for the pipeline and build process (if they’re not reused)

Conclusion

In this article, I demonstrated how to build a custom IAM policy for a Lambda CI/CD pipeline using AWS SAM and GitHub.

Using a custom policy helps ensure that the Code Pipeline adheres to the principle of least privilege, keeping your AWS resources more secure.

I hope you found this article both helpful and informative!

Thank you for reading!

Watch the video here:


?????? ℬ?????
??? ????????? ???????? ????????? & ????????? ?????????
????? ?????????? ?????????


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

 
Вверх Снизу