Building Robust Security, Identity, & Compliance Frameworks in AWS

Isaac Oppong-Amoah - Jan 13 - - Dev Community

Effective security, identity, and compliance strategies are essential for safeguarding cloud environments. This post offers valuable insights into best practices, along with a practical lab and accompanying lab code for AWS security automation.


Key Concepts: Security, Identity, and Compliance

Security:

Implementing robust protection mechanisms ensures data and workloads are safe from malicious actors. Examples include network segmentation, encryption, and regular vulnerability scans.

Identity:

IAM (Identity and Access Management) ensures that the right individuals and services have appropriate access to resources. Tools like MFA, federated identities, and least privilege access are critical.

Compliance:

Compliance frameworks, such as ISO 27001 or PCI DSS, mandate specific security practices. Automated tools and audits can streamline adherence.


Lab 1: Enforcing MFA for AWS IAM Users

Lab Objective:

Set up a policy requiring all IAM users to enable Multi-Factor Authentication (MFA).

Prerequisites:

  • An AWS account
  • Basic IAM knowledge

Steps:

1. Create a Policy to Enforce MFA:

Use the following JSON policy:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Deny",
            "Action": "*",
            "Resource": "*",
            "Condition": {
                "BoolIfExists": {
                    "aws:MultiFactorAuthPresent": "false"
                }
            }
        }
    ]
}
Enter fullscreen mode Exit fullscreen mode
  1. Go to the IAM Console.
  2. Select Policies > Create Policy.
  3. Paste the JSON and save the policy.

2. Attach the Policy to IAM Users:

  1. Navigate to the Users section in the IAM Console.
  2. Select the users to enforce MFA.
  3. Attach the newly created policy.

3. Test the Policy:

  1. Log in as a user without MFA enabled.
  2. Attempt to access an AWS service.
  3. Verify that access is denied.
  4. Enable MFA and retry the service access.

Lab Code: Automating MFA Enforcement with AWS CLI

# Step 1: Create the MFA Enforcement Policy
aws iam create-policy \  
    --policy-name MFAEnforcementPolicy \  
    --policy-document file://mfa-enforcement-policy.json

# Step 2: Attach the Policy to a User
aws iam attach-user-policy \  
    --user-name ExampleUser \  
    --policy-arn arn:aws:iam::aws:policy/MFAEnforcementPolicy

# Step 3: List Users Without MFA Enabled
aws iam list-users | jq '.Users[] | select(.PasswordLastUsed != null) | .UserName'

# Step 4: Enable MFA for a User
aws iam enable-mfa-device \  
    --user-name ExampleUser \  
    --serial-number arn:aws:iam::5535678310xx:mfa/ExampleDevice \  
    --authentication-code1 34689 \  
    --authentication-code2 71098
Enter fullscreen mode Exit fullscreen mode

Lab 2: Automating Security Group Rules with AWS Lambda

Lab Objective:

Create an AWS Lambda function that automatically adjusts security group rules based on incoming CloudWatch Events.

Prerequisites:

  • An AWS account
  • Familiarity with Lambda and CloudWatch

Steps:

1. Create a CloudWatch Event Rule:

  1. Navigate to the CloudWatch Console.
  2. Create a rule to capture specific API calls (e.g., AuthorizeSecurityGroupIngress).
  3. Set the target to invoke a Lambda function.

2. Create a Lambda Function:

Deploy the following Python function to manage security group rules dynamically:

Lab Code via AWS Lambd

import boto3

def lambda_handler(event, context):
    ec2 = boto3.client('ec2')
    security_group_id = event['detail']['requestParameters']['groupId']

    # Example: Revoke all inbound rules
    response = ec2.describe_security_groups(GroupIds=[security_group_id])
    for rule in response['SecurityGroups'][0]['IpPermissions']:
        ec2.revoke_security_group_ingress(
            GroupId=security_group_id,
            IpPermissions=[rule]
        )

    print(f"Cleared inbound rules for {security_group_id}")
Enter fullscreen mode Exit fullscreen mode
  1. Navigate to the Lambda Console.
  2. Create a new function and paste the code.
  3. Attach the necessary IAM role with ec2:DescribeSecurityGroups and ec2:RevokeSecurityGroupIngress permissions.

3. Test the Function:

  1. Trigger the CloudWatch Event.
  2. Verify that the Lambda function executed successfully and security group rules were updated.

Lab 3: Centralized Logging with Amazon CloudWatch and S3

Lab Objective:

Set up centralized logging by sending AWS CloudWatch logs to an S3 bucket for long-term storage and compliance.

Prerequisites:

  • An AWS account
  • Basic knowledge of CloudWatch and S3

Steps:

1. Create an S3 Bucket:

  1. Navigate to the S3 Console.
  2. Create a new bucket (e.g., lab-centralized-logs-bucket).
  3. Configure appropriate permissions to allow CloudWatch to write to the bucket.

2. Set Up CloudWatch Log Group:

  1. Navigate to the CloudWatch Console.
  2. Create or select an existing log group.
  3. Set up a subscription filter to send logs to the S3 bucket.

3. Verify Log Storage:

  1. Generate logs in the CloudWatch log group.
  2. Check the S3 bucket to confirm logs are being stored.

Lab Code: Automating Centralized Logging Setup with AWS CLI

# Step 1: Create an S3 Bucket
aws s3api create-bucket --bucket lab-centralized-logs-bucket --region us-east-1

# Step 2: Put a Bucket Policy
aws s3api put-bucket-policy --bucket lab-centralized-logs-bucket --policy file://bucket-policy.json

# Step 3: Set Up CloudWatch Log Group
aws logs create-log-group --log-group-name MyLogGroup

# Step 4: Create a Destination for Logs
aws logs put-destination \  
    --destination-name S3Logs \  
    --target-arn arn:aws:s3:::lab-centralized-logs-bucket
Enter fullscreen mode Exit fullscreen mode

Lab 4: Using AWS Config for Compliance Auditing

Lab Objective:

Implement AWS Config to monitor compliance and generate reports for resources violating best practices.

Prerequisites:

  • An AWS account
  • Basic knowledge of AWS Config

Steps:

1. Enable AWS Config:

  1. Navigate to the Config Console.
  2. Create a configuration recorder and delivery channel.
  3. Choose rules based on compliance requirements (e.g., restricted-common-ports).

2. View Compliance Reports:

  1. Navigate to the compliance dashboard in the Config Console.
  2. Analyze non-compliant resources and take action as needed.

3. Automate Remediation (Optional):

  1. Set up an AWS Lambda function for automatic remediation.
  2. Use Config rules to trigger the Lambda function when violations are detected.

Lab Code: Enabling AWS Config with CLI

# Step 1: Create a Configuration Recorder
aws configservice put-configuration-recorder \  
    --configuration-recorder-name default \  
    --role-arn arn:aws:iam::5535678310xx:role/AWSConfigRole

# Step 2: Start Recording
aws configservice start-configuration-recorder \  
    --configuration-recorder-name default

# Step 3: Add a Config Rule
aws configservice put-config-rule \  
    --config-rule-name restricted-common-ports \  
    --source "Owner=AWS,SourceIdentifier=RESTRICTED_COMMON_PORTS"
Enter fullscreen mode Exit fullscreen mode

Lab 5: Implementing Data Loss Prevention with Amazon Macie

Lab Objective:

Leverage Amazon Macie to identify and protect sensitive data stored in S3 buckets.

Prerequisites:

  • An AWS account
  • Familiarity with S3 and IAM

Steps:

1. Enable Amazon Macie:

  1. Navigate to the Macie Console.
  2. Enable Macie for your account.
  3. Assign the appropriate IAM roles to allow Macie access to S3 buckets.

2. Run a Data Discovery Job:

  1. Create a job in the Macie Console.
  2. Select the target S3 buckets.
  3. Configure the job to identify sensitive data, such as PII or financial information.

3. Review Findings:

  1. Navigate to the findings section in the Macie Console.
  2. Review and address flagged sensitive data.

Lab Code: Setting Up Macie Data Discovery Jobs with CLI

# Step 1: Enable Macie
aws macie2 enable-macie

# Step 2: Create a Classification Job
aws macie2 create-classification-job \  
    --job-type ONE_TIME \  
    --name SensitiveDataDiscovery \  
    --s3-job-definition '{"bucketDefinitions":[{"accountId":"5535678310xx","buckets":["my-sensitive-data-bucket"]}]}'

# Step 3: List Findings
aws macie2 list-findings

# Step 4: Get Finding Details
aws macie2 get-finding \  
    --finding-id 3457900x-34rt-433d-566h-45678770vbxx
Enter fullscreen mode Exit fullscreen mode

Conclusion

AWS provides a comprehensive suite of tools and services designed to automate and streamline security, identity management, and compliance processes. By automating critical security and compliance workflows, businesses can reduce human error, improve operational efficiency, and achieve consistent policy enforcement across their AWS environments.

Refrence
https://docs.aws.amazon.com/security/
https://docs.aws.amazon.com/iam/

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .