Use Python & AWS Lambda To Enforce Security Compliance in AWS in an Automated Way #Lambda #Python
**Enforce Security Compliance in AWS using Python & Lambda in an Automated Way** Automatically detect and terminate EC2 instances with SSH open to the public using a Python Lambda function triggered by EventBridge. 🔗 **Get the code here:** https://github.com/segunofe/EnforceSecurityAWSLambdaPythonAutomation.git Steps **Step 1 – Write the Lambda Function** Write your Python compliance logic and wrap it in a Lambda handler: `def lambda_handler(event, context)`. Select all your code and press Tab to indent it inside the handler. **Step 2 – Package & Upload to S3** Zip your Python file, upload it to an S3 bucket, and copy the Object URL for use in Step 4. **Step 3 – Create an IAM Role** Go to IAM, Roles, Create role. Select AWS Service, Lambda, name it dev-role-lambda, and attach AmazonEC2FullAccess and AWSLambdaBasicExecutionRole. **Step 4 – Create the Lambda Function** Name it open-ssh-ec2-check, set the runtime to Python 3.12, and assign the role from Step 3. Upload the code via the S3 URL, set the handler to open-ssh-ec2-check.lambda_handler, increase the timeout to 30 seconds, then run a test. **Step 5 – Add an EventBridge Trigger** Add a trigger using EventBridge. Create a new rule, set the event type to EC2 Instance State-change Notification, and filter by detail.state = "running". This fires the Lambda automatically whenever a new EC2 instance starts. *Like & subscribe for more !*
Download
0 formatsNo download links available.