
How I Built AWS Security Auto-Remediation with Terraform
Deploy CloudTrail, AWS Config, EventBridge, Lambda, and DynamoDB. Test real SSH/RDP exposure, verify the fixes, and safely tear everything down.
Series: Yashvi Kothari Portfolio Project on AWS Cloud Security DevOps (1 article)
- 1How I Built AWS Security Auto-Remediation with Terraform This article
How I Built AWS Security Auto-Remediation with Terraform
An end-to-end AWS CloudOps lab: detect public SSH/RDP exposure, automatically revoke unsafe access, record an audit trail, and destroy the infrastructure when you're done.



I opened SSH to the internet. Then I watched AWS close it.
One command introduced a security risk.
Another system removed it.
No engineer clicked Revoke rule. No scheduled compliance job needed to discover the change first. An event-driven Lambda function inspected the Security Group, removed the unsafe public ingress, and recorded what happened in DynamoDB.
The important part wasn't simply that automation worked.
It fixed the dangerous rule without deleting the legitimate one.
On November 13, 2026, my AWS lab successfully demonstrated three remediation events: public SSH detected through CloudTrail, public RDP detected through CloudTrail, and public SSH detected through AWS Config.
Each resulted in a DynamoDB audit record. An additional Lambda replay returned
IDEMPOTENT_SKIP , confirming that reprocessing an already-safe Security Group did not cause another revocation.
This article shows how to build the same architecture with Terraform and Python, reproduce the tests, understand the failures I encountered, and clean up afterward.

1. The problem: detection without correction
A Security Group protects access to AWS resources by controlling inbound and outbound traffic.
Suppose an engineer temporarily permits SSH from anywhere:

That CIDR includes every IPv4 address. If the Security Group is attached to a reachable instance, this can unnecessarily expose SSH to the internet.
Traditional monitoring may identify the mistake, create a finding, or send an alert.
But someone still has to respond.
I wanted to build a system that could answer four questions automatically:
- What changed?
- Is the change unsafe?
- Can I reverse only the unsafe part?
- Can I prove exactly what I changed?
The lab focused on TCP port 22 (SSH) and 3389 (RDP), with public IPv4
0.0.0.0/0 and IPv6 ::/0 exposure.For safety, I tested against a dedicated, unattached Security Group inside the account's default VPC. No EC2 instance depended on the group.
The Security Group also contained a simulated corporate SSH rule:

This rule was intentionally kept throughout remediation.
The requirement was not “delete SSH.”
It was “remove public SSH exposure while preserving approved access.”
2. Architecture: two signals, one decision engine
I compared two implementation patterns.
The first was AWS Config with Systems Manager Automation. It's useful when you want managed compliance evaluation tied to a predefined remediation document.
The alternative was event-driven remediation using CloudTrail, AWS Config, EventBridge, and Lambda.
I chose the second pattern because it allowed precise control over parsing, rule removal, failure handling, logging, and audit receipts. It also avoided relying on Systems Manager Automation permissions that were restricted in the environment I explored.
The system has two complementary inputs.
Path A: API-change detection
An engineer adds an ingress rule.
The EC2
AuthorizeSecurityGroupIngress API activity is captured through CloudTrail and reaches the default EventBridge event bus.An EventBridge rule matches the event and invokes Lambda.
Path B: compliance-change detection
AWS Config evaluates Security Groups against its managed
INCOMING_SSH_DISABLED rule.If a relevant resource becomes
NON_COMPLIANT , an AWS Config compliance-change event can invoke the same Lambda.Both paths converge on one remediation engine, which checks the current Security Group state before making changes.

Why both CloudTrail and AWS Config?
CloudTrail provides visibility into the API action that introduced the rule.
AWS Config provides a separate compliance-evaluation perspective.
That distinction matters. An API event tells us what someone attempted or changed. A compliance evaluation tells us whether the resulting configuration violates a defined rule.
AWS Config evaluation is asynchronous, and EventBridge service-event delivery is best effort. Neither path guarantees a fixed remediation latency.
There is another important distinction: AWS Config's
INCOMING_SSH_DISABLED rule evaluates public SSH exposure, while our Lambda also handles public RDP exposure. RDP remediation in this lab was demonstrated through the CloudTrail path.3. Prerequisites and project setup
I used Windows PowerShell, AWS CLI, Terraform, Python Lambda code, and AWS resources in
us-east-1 .You will need:
- An AWS account you are authorized to use, preferably a disposable lab account.
- AWS CLI v2 configured with credentials that can provision the required services.
- Terraform 1.5 or newer.
- Git and a terminal. PowerShell works for the commands below.
- A default VPC in
us-east-1, or an adapted Terraform configuration that targets another VPC.
- A billing budget alert. The services in this project can incur charges.
First, clone the repository:
git clone https://github.com/yashvikothari/aws-event-driven-auto-remediation.git
cd aws-event-driven-auto-remediationSet your AWS CLI profile. Replace the example with your own profile name:
$env:AWS_PROFILE = "YOUR_AWS_PROFILE"$env:AWS_DEFAULT_REGION = "us-east-1"
aws sts get-caller-identityConfirm that the returned account is the one you intend to use.
The important repository files are:

The Terraform files create the infrastructure.
src/remediate.py contains the remediation logic. The test event and scripts support verification and troubleshooting.The optional
.sh scripts are intended for Bash environments; the PowerShell examples in this article work independently of them.Check AWS Config before deployment
AWS Config recorder configuration is an account-and-Region-level concern.
Before deploying, inspect the existing recorders:

If a suitable recorder already exists, use the repository's
enable_config_recorder=false Terraform variable instead of trying to create a conflicting one.Also verify that the existing recorder captures
AWS::EC2::SecurityGroup resources.This preflight matters because Terraform cannot safely create a second conflicting recorder just because the lab expects one.
4. Build the infrastructure with Terraform
The full Terraform source is in main.tf . I'll highlight the components that explain how the system works.
Step 1: Create a controlled Security Group
Terraform creates a dedicated test Security Group in the default VPC.
Its initial inbound access is restricted to a simulated corporate CIDR:

The
10.20.0.0/16 entry is a test fixture, not an instruction to use that network as your organization's allowlist.Design decision: Rather than edit an existing production Security Group, Terraform owns an isolated group that can be destroyed at the end of the exercise.
The lab's original implementation was built around this disposable target. During testing, I reused that same unattached group instead of creating extra groups and increasing the cleanup surface.
Step 2: Create a DynamoDB audit ledger
Every successful remediation should leave evidence.
The project uses DynamoDB on-demand capacity and a conditional-write audit design.

On-demand billing is convenient for an unpredictable, low-volume lab.
The
ExpiresAt attribute supports a 90-day TTL in the Lambda code, reducing long-term storage accumulation.A conditional DynamoDB write helps prevent an existing receipt from being overwritten. It is not the same as a legally immutable, write-once audit store.
Step 3: Configure CloudTrail and S3 telemetry
Terraform provisions a CloudTrail trail and an S3 bucket for telemetry.
The S3 configuration includes public-access blocking and default SSE-S3 encryption.
The trail records management activity, including Security Group API mutations.
For this disposable lab, the bucket uses:
force_destroy = true
That simplifies teardown, but it also means Terraform can delete telemetry objects when destroying the bucket.
Do not use this setting casually for production audit storage. Production evidence normally needs explicit retention and deletion controls.
Step 4: Enable AWS Config compliance evaluation
The AWS Config managed rule uses the source identifier
INCOMING_SSH_DISABLED .

Where the account doesn't already have a suitable recorder, the project also creates the recorder, its service role, a delivery channel, and the recorder status resource.
When a compatible recorder already exists, deployment must be adapted to use it without disturbing unrelated configuration.
Note: The managed rule's identifier is
INCOMING_SSH_DISABLED ; its behavior is to mark public SSH exposure as noncompliant. A restricted source CIDR such as 10.20.0.0/16 can remain compliant.Step 5: Configure the dual-trigger EventBridge rule
This is the point where the architecture becomes event-driven.
The Terraform rule matches either an EC2 API event or an AWS Config noncompliance event:
event_pattern = jsonencode({ "$or" = [ { source = ["aws.ec2"] "detail-type" = ["AWS API Call via CloudTrail"]
detail = { eventSource = ["ec2.amazonaws.com"] eventName = ["AuthorizeSecurityGroupIngress"] } }, { source = ["aws.config"] "detail-type" = ["Config Rules Compliance Change"]
detail = { configRuleName = [ aws_config_config_rule.restricted_ssh.name ]
newEvaluationResult = { complianceType = ["NON_COMPLIANT"] } } } ]})The rule's target is our Lambda function.
Terraform also creates an
aws_lambda_permission resource allowing EventBridge to invoke it.The
$or is important because the two event types have different structures. Trying to match them as one flat event pattern would not express the same logic.[ EVENTBRIDGE CONFIGURATION]

showing CloudTrail and Config matching branches.

showing the Lambda target.
Step 6: Scope Lambda's IAM permissions
The Lambda runs using Python 3.12, with 128 MB of memory and a five-second timeout in this lab.
Its environment variables include:
environment { variables = { AUDIT_TABLE_NAME = aws_dynamodb_table.audit_ledger.name PROTECTED_PORTS = "22,3389" ALLOWED_SG_ID = aws_security_group.test_sg.id }}The dedicated execution role can describe Security Groups, revoke ingress and tag the one test group, write to the audit table, and emit CloudWatch logs.
The most important write permission is scoped to the actual Security Group ARN:
{ Sid = "FixOnlyDemoSecurityGroup" Effect = "Allow"
Action = [ "ec2:RevokeSecurityGroupIngress", "ec2:CreateTags" ]
Resource = aws_security_group.test_sg.arn}This is the central security boundary: Lambda should not be able to revoke access on arbitrary Security Groups.
The deployment IAM identity has broader infrastructure-creation permissions. It is separate from the Lambda execution role and should not be confused with the runtime's least-privilege policy.
Step 7: Deploy
Run Terraform's validation and deployment flow:
terraform initterraform fmt -recursiveterraform validate
terraform plan -out demo.tfplanterraform apply demo.tfplanIf you're using an existing AWS Config recorder, pass the appropriate variable during planning:
terraform plan ` -var="enable_config_recorder=false" ` -out demo.tfplan
terraform apply demo.tfplanUse one plan variant or the other not both.
Review the plan before applying, especially the Security Group target, AWS Config resources, IAM roles, CloudTrail, and S3 bucket.
On successful deployment, capture Terraform's outputs:
terraform output
$sg = terraform output -raw target_security_group_id$lambda = terraform output -raw lambda_function_name$table = terraform output -raw dynamodb_audit_table$configRule = terraform output -raw config_rule_name[ TERRAFORM DEPLOYMENT]

faced IAM user permission issue ,middle of terraform tainting issue also came solved that

successful Terraform apply screenshot showing the completed apply and outputs.

showing managed resource inventory.
5. The Lambda: remove only what is dangerous
A remediation function can become a bigger risk than the misconfiguration it fixes.
Imagine a Lambda that sees a public SSH rule and responds by deleting every inbound SSH permission. The internet exposure disappears, but approved administrative access disappears too.
Our Lambda takes a narrower approach.
Its workflow is:
Parse the event → verify the target → inspect current rules → identify public protected-port access → revoke only violating CIDRs → tag the Security Group → write an audit receipt.
First, normalize two different event formats
A CloudTrail-delivered EC2 event exposes the Security Group ID through
requestParameters .An AWS Config compliance event can carry the resource ID through different nested fields.
The handler normalizes them:
def extract_event_context(event): detail = event.get("detail") or {}
if event.get("source") == "aws.ec2": group_id = ( (detail.get("requestParameters") or {}) .get("groupId") ) actor = ( (detail.get("userIdentity") or {}) .get("arn") or "unknown-actor" ) return group_id, "CLOUDTRAIL_REALTIME", actor
if event.get("source") == "aws.config": evaluation = detail.get("newEvaluationResult") or {} qualifier = ( (evaluation.get("evaluationResultIdentifier") or {}) .get("evaluationResultQualifier") or {} )
group_id = ( detail.get("resourceId") or qualifier.get("resourceId") or (detail.get("configurationItem") or {}) .get("resourceId") ) return group_id, "AWS_CONFIG", "config.amazonaws.com"
return None, "UNSUPPORTED_SOURCE", "unknown"This normalization is not just code hygiene. It came from an actual failure during testing, which I'll explain later.
Next, enforce the target boundary
Before inspecting or modifying anything, the Lambda compares the event's Security Group ID with
ALLOWED_SG_ID .if group_id != ALLOWED_SG_ID: result = { "status": "IGNORED", "reason": "Not the protected demo security group", "group_id": group_id } print(json.dumps(result)) return resultEven if the EventBridge rule matches an API call affecting another Security Group, this guard prevents the function from acting on it.
Then, revoke only public source ranges
The handler examines the Security Group's current
IpPermissions .For TCP rules, it checks whether the port range includes 22 or 3389. An all-protocol rule also qualifies because it exposes those ports.
It filters only the public CIDRs:
PUBLIC_CIDRS = {"0.0.0.0/0", "::/0"}
public_ipv4 = [ r for r in permission.get("IpRanges", []) if r.get("CidrIp") in PUBLIC_CIDRS]
public_ipv6 = [ r for r in permission.get("Ipv6Ranges", []) if r.get("CidrIpv6") in PUBLIC_CIDRS]The code constructs a revoke request containing only those public entries.
It does not revoke
10.20.0.0/16 .And if there is nothing unsafe to remove, it returns:
{ "status": "IDEMPOTENT_SKIP", "reason": "No public SSH/RDP ingress rules to remove"}The implementation also handles the EC2
InvalidPermission.NotFound error as an idempotent skip when another invocation has already removed the rule.Finally, write the audit receipt
After revocation, Lambda tags the Security Group with
AutoRemediated , LastRemediatedAt , and RemediationSource .It then writes a record to DynamoDB using:
ConditionExpression="attribute_not_exists(AuditId)"The receipt includes the Security Group ID, trigger source, actor, removed permissions, remediation timestamp, and TTL.
This protects the existing receipt from a duplicate conditional write.
There is a design limitation worth noting: the current
AuditId is derived from the Security Group ID and the current UTC minute. Two separate incidents on the same group within the same minute could collide. A production design should use a unique event or incident identifier and a stronger audit-retention strategy.6. Live test: break the rule, then verify the fix
Now we move from infrastructure code to actual behavior.
The following commands deliberately introduce unsafe ingress. Use them only on the dedicated, unattached lab Security Group returned by Terraform.
First, inspect the baseline:
aws ec2 describe-security-groups ` --group-ids $sg ` --query "SecurityGroups[0].IpPermissions" ` --region us-east-1 ` --output jsonThe baseline should contain the restricted SSH source
10.20.0.0/16 , not a public SSH rule.Test A: Public SSH
Introduce TCP port 22 from anywhere:
aws ec2 authorize-security-group-ingress ` --group-id $sg ` --protocol tcp ` --port 22 ` --cidr 0.0.0.0/0 ` --region us-east-1This produces a real EC2 API event.
EventBridge matches the
AuthorizeSecurityGroupIngress event and invokes Lambda.After event delivery and processing, inspect the Security Group again:
aws ec2 describe-security-groups ` --group-ids $sg ` --query "SecurityGroups[0].IpPermissions" ` --region us-east-1 ` --output jsonThe expected outcome:
Public SSH 0.0.0.0/0 → RemovedRestricted SSH 10.20.0.0/16 → PreservedIn my live test, the Lambda returned
REMEDIATED with CLOUDTRAIL_REALTIME as the trigger source. Its reported execution measurement was approximately 1,070 ms.That measures time inside the Lambda invocation not the entire exposure-to-remediation window.
[LIVE DRIFT TEST]


CloudTrail test showing public SSH being authorized and then absent on the follow-up Security Group query.


Test B: Public RDP
Once the SSH test is complete, repeat with TCP port 3389:
aws ec2 authorize-security-group-ingress ` --group-id $sg ` --protocol tcp ` --port 3389 ` --cidr 0.0.0.0/0 ` --region us-east-1Verify the same Security Group state.
The RDP exposure should disappear, while restricted SSH remains unchanged.
The real lab produced a separate CloudTrail-triggered RDP remediation receipt.
Verify in the EC2 console
Go to EC2 → Security Groups → your Terraform-created Security Group → Inbound rules.
You should see the authorized restricted SSH rule but no public SSH or RDP entry.
The resource tags should also show the remediation metadata.
[ SURGICAL REMEDIATION]

_ shows
10.20.0.0/16 preserved. _

_ shows
AutoRemediated=true and the remediation source._This is the difference between a successful security outcome and a destructive automation.
Removing danger is useful. Removing danger without breaking approved access is engineering.
7. Prove the AWS Config path independently
The CloudTrail tests succeeded, but that raised another question.
Was AWS Config genuinely triggering remediation, or was CloudTrail always getting there first?
Both paths normally listen to the same Security Group activity. The API-event path can remediate the group before AWS Config emits a noncompliance change.
That makes the two paths difficult to distinguish if you only look at the final Security Group.
To test AWS Config independently, I temporarily replaced the EventBridge rule's dual-trigger pattern with a Config-only pattern.
First, back up the original pattern:
$rule = $lambda -replace "-engine$", "-sg-drift-rule"
aws events describe-rule ` --name $rule ` --query EventPattern ` --output text ` --region us-east-1 | Out-File .\eventbridge-original.json -Encoding asciiNext, create the Config-only test pattern:
$configOnly = @{ source = @("aws.config") "detail-type" = @("Config Rules Compliance Change") detail = @{ configRuleName = @($configRule) newEvaluationResult = @{ complianceType = @("NON_COMPLIANT") } }}
$configOnly | ConvertTo-Json -Depth 8 -Compress | Out-File .\config-only.json -Encoding asciiApply it temporarily:
aws events put-rule ` --name $rule ` --event-pattern file://config-only.json ` --region us-east-1Then introduce public SSH on the same unattached lab group and allow AWS Config time to evaluate the change.
This time, the successful Lambda result explicitly identified:
{ "status": "REMEDIATED", "trigger_source": "AWS_CONFIG", "violating_actor": "config.amazonaws.com", "ledger_status": "RECORDED", "latency_ms": 936.84}The revoked permission was TCP 22 from
0.0.0.0/0 .This was stronger evidence than simply proving that the EventBridge event pattern matched a sample payload. It demonstrated a real Config-triggered Lambda invocation and a matching DynamoDB receipt.
After the test, I restored the original dual-trigger pattern:
aws events put-rule ` --name $rule ` --event-pattern file://eventbridge-original.json ` --region us-east-1Verify that the restored event pattern includes both
aws.ec2 and aws.config .Restoration is part of the test, not an optional cleanup detail. Leaving the rule in Config-only mode would silently remove the CloudTrail detection path.
[AWS CONFIG LIVE PROOF]

shows
trigger_source: AWS_CONFIG , the revoked public SSH permission, ledger_status: RECORDED , and 936.84 ms.I also checked the AWS Config console. The managed rule eventually reported COMPLIANT after the unsafe ingress was removed.
[AWS CONFIG COMPLIANCE]

showing the
INCOMING_SSH_DISABLED managed rule in the compliant state.The console displayed Remediation action: Not set. That was expected because we implemented remediation through EventBridge and Lambda, not AWS Config's native remediation-action configuration.
8. Verify the DynamoDB receipts and idempotency
Removing the unsafe rule proves one part of the system.
Auditing proves another.
To inspect the audit table:
aws dynamodb scan ` --table-name $table ` --region us-east-1 ` --query "Items[].{AuditId:AuditId.S,Source:TriggerSource.S,When:RemediatedAt.S}" ` --output tableThe final lab audit scan contained three remediation receipts:
Test | Trigger source | Remediated exposure |
|---|---|---|
Public SSH | CLOUDTRAIL_REALTIME | TCP 22 |
Public RDP | CLOUDTRAIL_REALTIME | TCP 3389 |
Public SSH | AWS_CONFIG | TCP 22 |
The records identify the Security Group, source, actor, time, and revoked permissions.
[INSERT SCREENSHOT 8 — DYNAMODB AUDIT]

shows the Config receipt.

What happens if the same event is processed again?
Real event-driven systems must tolerate duplicate delivery and retries.
We tested that behavior in two ways.
First, a safe Lambda re-invocation returned
IDEMPOTENT_SKIP because the public rule had already been removed.Second, a duplicate DynamoDB write using an existing
AuditId failed with:ConditionalCheckFailedExceptionThat was the expected outcome, not a malfunction.
It demonstrated that the conditional write prevented overwriting the original receipt.
[INSERT SCREENSHOT 9 — IDEMPOTENCY]
_
15-Lambda-Idempotency-Runtime-Test-PASS.png _
16-DynamoDB-Duplicate-Protection-PASS.png
safe replay and conditional audit-write protection.
9. Observe the system with CloudWatch Logs Insights
Security automation needs an operational feedback loop.
You need to know not just whether Lambda ran, but what it decided.
Open CloudWatch → Logs Insights and select the Lambda log group, named
/aws/lambda/<lambda-function-name> .Run:
fields @timestamp, status, security_group_id, trigger_source, violating_actor, latency_ms, ledger_status| filter status = "REMEDIATED"| sort @timestamp desc| limit 20This helps surface successful remediation activity and the associated runtime measurements.
In my evidence capture, CloudWatch Logs Insights displayed two matching
REMEDIATED rows, including CloudTrail and AWS Config activity.DynamoDB independently showed three receipts. Those are different observations and should not be presented as an identical count.
[CLOUDWATCH LOGS INSIGHTS]
_
CloudWatch-Logs-Insights-Remediation-Evidence Shows the query and two result rows. _
The logged latency field describes Lambda execution duration. It does not establish a guaranteed end-to-end remediation time.
For production monitoring, I would add separate measurements for event arrival, remediation completion, failures, retry attempts, and unresolved exposures.
10. What broke and what I learned fixing it
The clean architecture diagram hides most of the engineering work.
My implementation wasn't a straight line from
terraform apply to success.Several failures forced better design decisions.
Failure 1: The Systems Manager permission trap
An early remediation approach involved AWS Config and Systems Manager Automation.
The problem was practical: the environment did not have all the permissions needed to run the intended Automation document.
Depending on the account,
ssm:StartAutomationExecution , the Automation execution role, and related access can be restricted.I did not solve that problem by requesting unrestricted access to everything.
Instead, I built the remediation operation directly in Lambda, using scoped EC2 API permissions.
Lesson: Choose an implementation that fits the actual permission boundary. A theoretically elegant managed workflow isn't useful if the environment cannot execute it safely.
Failure 2: One Lambda, two incompatible event payloads
The next failure was a Python
KeyError involving requestParameters .The initial parsing logic expected the CloudTrail structure.
But AWS Config compliance events don't carry the same fields.
This fails when the expected key is absent:
group_id = event["detail"]["requestParameters"]["groupId"]The solution was to branch on the event source and normalize the group ID before proceeding.
That produced the
extract_event_context() function shown earlier.[PARSER FAILURE AND FIX]
Lesson: Event schemas are contracts. Never assume two services describe the same resource using the same JSON structure.
Failure 3: A naive fix can break legitimate traffic
Consider an ingress rule that combines multiple source CIDRs.
If the remediation code revokes an entire permission indiscriminately, it can delete approved access along with the public source.
The solution was to reconstruct the revoke request with only
0.0.0.0/0 or ::/0 entries.The safe corporate source remained untouched during remediation.
Lesson: The smallest correct change is usually safer than the broadest possible rollback.
Failure 4: Event pattern validation isn't live delivery
I validated the AWS Config event pattern with EventBridge's
TestEventPattern functionality.It matched the supplied event structure.
But a matching pattern only proves that the pattern matches that sample.
It does not prove that AWS Config emitted a real event, EventBridge delivered it, Lambda executed, or the Security Group changed.
I also attempted to publish a synthetic event using the reserved
aws.config source. EventBridge rejected it with NotAuthorizedForSourceException .Rather than treat that synthetic failure as a production problem, I used the Config-only isolation test described earlier.
That produced genuine runtime and audit evidence.
pattern-validation evidence
Lesson: Test the event path end to end. A green configuration test is not the same as a successful remediation.
Failure 5: IAM permissions can hide observability
During testing, my deployment identity lacked permissions for some inspection operations, including CloudWatch metrics discovery.
Instead of expanding IAM access just to collect one optional screenshot, I used the logs and audit records that were already accessible.
This also highlighted the difference between deployment/test permissions and Lambda's execution permissions.
Lesson: Observability should be designed, not improvised through broad administrator access.
Failure 6: Terraform plans need careful reading
During deployment, Terraform surfaced resource-creation and replacement plans, including changes involving the telemetry bucket.
I treated the plans as operations to review not commands to accept blindly.
An S3 replacement matters because telemetry data may be deleted, and the lab configuration explicitly enables forced bucket cleanup.
Lesson: Infrastructure as code makes destructive actions reproducible. That makes reviewing the plan more important, not less.
11. Production thinking: what I would improve
This was a successful controlled lab, not a production-ready security platform.
There are several design choices I would revisit before expanding its scope.
Security
The Lambda execution role is scoped to a single test Security Group. That is deliberate.
For production, I would use an approved inventory of protected resources, explicit ownership rules, authorization boundaries, and change-management controls.
I would also validate more ingress configurations, including different protocol and port-range combinations, before automatically modifying business-critical resources.
Reliability
EventBridge and AWS Config are asynchronous systems, and events can be delayed or duplicated.
The current implementation checks Security Group state before acting and handles already-revoked rules.
For a production version, I'd add a dead-letter queue, clear retry policies, failure alarms, and a reconciliation job that periodically checks whether unsafe exposure remains.
Audit design
DynamoDB conditional writes provide useful duplicate protection.
But the current minute-based audit key is too coarse for a higher-volume system, and the table's TTL makes it a temporary audit store.
I would move to unique incident IDs, more durable audit retention, and stronger controls over who can alter or delete records.
Coverage
The CloudTrail filter detects
AuthorizeSecurityGroupIngress , while AWS Config provides a second signal for public SSH compliance changes.This does not represent every possible Security Group mutation or every dangerous port.
The lab also does not demonstrate automatic native AWS Config remediation or a fixed latency service-level objective.
A broader implementation would need more complete event coverage and additional test cases.

Cost
The project uses several potentially billable services: AWS Config, CloudTrail, S3, DynamoDB, Lambda, and CloudWatch Logs.
The lab used on-demand DynamoDB, a small Lambda configuration, a disposable telemetry bucket, and a $1 AWS Budget alert.
A budget alert is a notification, not a spending cap.
Costs vary by Region, service usage, event volume, retention and account conditions. Before deploying, review current AWS pricing and set a budget appropriate to your account.
The most reliable cost control for a temporary lab is to verify cleanup when the work is finished.
12. Clean up: the final test of your infrastructure
A project isn't finished when the demo succeeds.
It's finished when you can explain what remains in the account.
Before destroying the stack, I backed up the S3 telemetry locally. The final backup contained 160 files, totaling 885,664 bytes.
Those files may contain account and security-event metadata. They should not be uploaded to a public GitHub repository.
Then I prepared Terraform's destroy plan:
terraform plan -destroy -out destroy.tfplanThe actual lab destroy plan reported:
Plan: 0 to add, 0 to change, 20 to destroy.After reviewing the plan, apply it:
terraform apply destroy.tfplanFor a normal disposable run, you can also use
terraform destroy after reviewing the changes.[CLEANUP TERRAFORM DESTROY]

. shows successful destruction.
Verify the state
After the destroy operation succeeds, run:
terraform state listThe expected result is no remaining managed lab resources.
Do not treat an empty Terraform state as the only verification. Terraform can miss resources created manually or outside its state.
Check the relevant AWS services as well.
The leftover CloudWatch log group
In this lab, Lambda's CloudWatch log group remained after Terraform teardown.
That happened because the group had been created at runtime and was not managed as a Terraform log-group resource.
I deleted it separately and verified the result using:
aws logs describe-log-groups ` --log-group-name-prefix "/aws/lambda/$lambda" ` --region us-east-1 ` --query "logGroups[].logGroupName" ` --output jsonAfter cleanup, the command returned:
[]If your own lab leaves that log group behind, remove it after preserving any evidence you need:
aws logs delete-log-group ` --log-group-name "/aws/lambda/$lambda" ` --region us-east-1Verify again with
describe-log-groups .[LEFTOVER CLEANUP]


showing the deleted Lambda log group and verification result.
This final check matters. CloudWatch log storage can continue to incur charges after a Lambda function is deleted.
I retained the AWS Budget alert for future labs. IAM identity and custom-policy cleanup was considered separately from Terraform-managed resources.
13. What the live implementation proved
Here is what I could verify not what the architecture merely promised.

The AWS Config execution recorded approximately 936.84 ms of Lambda processing time. The CloudTrail SSH execution recorded approximately 1,070.12 ms.
These measurements describe individual function executions, not guaranteed end-to-end recovery times.
The more meaningful result was architectural:
An unsafe Security Group rule was introduced, detected, selectively removed, and documented without deleting the approved SSH rule.
The takeaway
The difference between a security alert and a security control is action.
The difference between automation and trustworthy automation is restraint.
CloudTrail and AWS Config provide the signals. EventBridge routes them. Lambda makes the decision. EC2 applies the correction. DynamoDB and CloudWatch provide evidence.
Terraform makes the entire experiment reproducible and gives us a way to remove it when we're finished.
The hardest part wasn't creating the AWS resources.
It was handling two incompatible event formats, navigating permission boundaries, preventing collateral damage, proving both detection paths, and checking what remained after teardown.
Those are the details that turn a demo into a useful engineering lesson.
Build it. Break it safely. Verify what changed. Record the evidence. Destroy the lab.
That is a complete CloudOps workflow.

Source code and references
Terraform implementation: https://github.com/yashvikothari/aws-event-driven-auto-remediation/blob/main/main.tf
Lambda source: https://github.com/yashvikothari/aws-event-driven-auto-remediation/blob/main/src/remediate.py
Windows setup guide: https://github.com/yashvikothari/aws-event-driven-auto-remediation/blob/main/WINDOWS\_BONUS\_GUIDE.md
AWS Config
INCOMING_SSH_DISABLED rule: https://docs.aws.amazon.com/config/latest/developerguide/restricted-ssh.html AWS Config events in EventBridge: https://docs.aws.amazon.com/eventbridge/latest/ref/events-ref-config.html
Series: Yashvi Kothari Portfolio Project on AWS Cloud Security DevOps (1 article)
- 1How I Built AWS Security Auto-Remediation with Terraform This article
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article