|
| 1 | +--- |
| 2 | +displayed_sidebar: null |
| 3 | +description: Learn how to automatically detect and remediate insecure cloud resources using Port's AI capabilities and Claude Code to generate infrastructure-as-code patches. |
| 4 | +--- |
| 5 | + |
| 6 | +# Auto-remediate insecure cloud resources with AI |
| 7 | + |
| 8 | +Many organizations have strict policies requiring that all cloud resources meet specific security standards: |
| 9 | + |
| 10 | +- **Data storage** must be encrypted at rest |
| 11 | +- **S3 buckets** must not be publicly accessible |
| 12 | +- **ElastiCache** must have deletion protection enabled |
| 13 | +- **RDS instances** must be private and encrypted |
| 14 | + |
| 15 | +Relying on manual checks or ad-hoc fixes is error-prone and delays remediation. With Port + Claude Code, you can enforce policies at creation time and generate infrastructure-as-code (IaC) patches automatically. |
| 16 | + |
| 17 | +This guide demonstrates how to create an AI-powered system that automatically detects insecure cloud resources and generates Terraform patches to remediate security violations. |
| 18 | + |
| 19 | + |
| 20 | +## Common use cases |
| 21 | + |
| 22 | +- **Enforce security policies** by detecting and fixing unencrypted storage, public access, or missing deletion protection |
| 23 | +- **Reduce manual security reviews** by automating the detection and remediation of common misconfigurations |
| 24 | +- **Maintain compliance** by ensuring all cloud resources meet security requirements automatically |
| 25 | + |
| 26 | + |
| 27 | +## Prerequisites |
| 28 | + |
| 29 | +This guide assumes the following: |
| 30 | +- You have access to Port and have completed the [onboarding process](https://docs.port.io/getting-started/overview) |
| 31 | +- You have installed [Port's AWS integration](https://docs.port.io/build-your-software-catalog/sync-data-to-catalog/cloud-providers/aws) (or [GCP](https://docs.port.io/build-your-software-catalog/sync-data-to-catalog/cloud-providers/gcp/)/[Azure](https://docs.port.io/build-your-software-catalog/sync-data-to-catalog/cloud-providers/azure/)) |
| 32 | +- You have completed the setup in the [Trigger Claude Code from Port guide](https://docs.port.io/guides/all/trigger-claude-code-from-port) |
| 33 | + |
| 34 | +:::tip Multi-cloud and multi-agent support |
| 35 | +While this guide focuses on AWS RDS instances and uses Claude Code, the same approach can be applied to other cloud providers and resource types by adjusting the blueprint schemas and security policies. You can also use other AI coding agents like GitHub Copilot or Gemini to generate the infrastructure-as-code patches. |
| 36 | +::: |
| 37 | + |
| 38 | + |
| 39 | +## Set up data model |
| 40 | + |
| 41 | +We need to create blueprints to support our cloud resource security workflow. These blueprints will track cloud resources and their security compliance status. |
| 42 | + |
| 43 | +### Create RDS Instance blueprint |
| 44 | + |
| 45 | +When installing the AWS integration in Port, the `AWS Account` blueprint is created by default. |
| 46 | +However, the `RDS Instance` blueprint is not created automatically so we will need to create it manually. |
| 47 | + |
| 48 | +1. Go to the [builder](https://app.getport.io/settings/data-model) page of your portal. |
| 49 | +2. Click on `+ Blueprint`. |
| 50 | +3. Click on the `{...} Edit JSON` button. |
| 51 | +4. Copy and paste the following JSON configuration: |
| 52 | + |
| 53 | + <details> |
| 54 | + <summary><b>RDS Instance blueprint (Click to expand)</b></summary> |
| 55 | + |
| 56 | + ```json showLineNumbers |
| 57 | + { |
| 58 | + "identifier": "rdsInstance", |
| 59 | + "description": "This blueprint represents an AWS RDS DBInstance in our software catalog", |
| 60 | + "title": "RDS Instance", |
| 61 | + "icon": "AWS", |
| 62 | + "schema": { |
| 63 | + "properties": { |
| 64 | + "link": { |
| 65 | + "type": "string", |
| 66 | + "format": "url", |
| 67 | + "title": "Link" |
| 68 | + }, |
| 69 | + "dbInstanceClass": { |
| 70 | + "type": "string", |
| 71 | + "title": "DB Instance Class" |
| 72 | + }, |
| 73 | + "dbInstanceStatus": { |
| 74 | + "type": "string", |
| 75 | + "title": "DB Instance Status" |
| 76 | + }, |
| 77 | + "engine": { |
| 78 | + "type": "string", |
| 79 | + "title": "Engine" |
| 80 | + }, |
| 81 | + "storageType": { |
| 82 | + "type": "string", |
| 83 | + "title": "Storage Type" |
| 84 | + }, |
| 85 | + "engineVersion": { |
| 86 | + "type": "string", |
| 87 | + "title": "Engine Version" |
| 88 | + }, |
| 89 | + "port": { |
| 90 | + "type": "number", |
| 91 | + "title": "Port" |
| 92 | + }, |
| 93 | + "allocatedStorage": { |
| 94 | + "type": "number", |
| 95 | + "title": "Allocated Storage" |
| 96 | + }, |
| 97 | + "endpoint": { |
| 98 | + "type": "string", |
| 99 | + "title": "Endpoint" |
| 100 | + }, |
| 101 | + "multiAZ": { |
| 102 | + "type": "boolean", |
| 103 | + "title": "Multi-AZ" |
| 104 | + }, |
| 105 | + "deletionProtection": { |
| 106 | + "type": "boolean", |
| 107 | + "title": "Deletion Protection" |
| 108 | + }, |
| 109 | + "availabilityZone": { |
| 110 | + "type": "string", |
| 111 | + "title": "Availability Zone" |
| 112 | + }, |
| 113 | + "masterUsername": { |
| 114 | + "type": "string", |
| 115 | + "title": "Master Username" |
| 116 | + }, |
| 117 | + "publicAccess": { |
| 118 | + "type": "boolean", |
| 119 | + "title": "Public Access" |
| 120 | + }, |
| 121 | + "vpcSecurityGroups": { |
| 122 | + "type": "array", |
| 123 | + "items": { |
| 124 | + "type": "string" |
| 125 | + }, |
| 126 | + "title": "VPC Security Groups" |
| 127 | + }, |
| 128 | + "arn": { |
| 129 | + "type": "string", |
| 130 | + "title": "ARN" |
| 131 | + }, |
| 132 | + "storageEncrypted": { |
| 133 | + "icon": "DefaultProperty", |
| 134 | + "type": "boolean", |
| 135 | + "title": "Storage Encrypted" |
| 136 | + } |
| 137 | + }, |
| 138 | + "required": [] |
| 139 | + }, |
| 140 | + "mirrorProperties": {}, |
| 141 | + "calculationProperties": {}, |
| 142 | + "aggregationProperties": {}, |
| 143 | + "relations": { |
| 144 | + "account": { |
| 145 | + "title": "Account", |
| 146 | + "target": "awsAccount", |
| 147 | + "required": true, |
| 148 | + "many": false |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + ``` |
| 153 | + </details> |
| 154 | + |
| 155 | +5. Click `Create` to save the blueprint. |
| 156 | + |
| 157 | + |
| 158 | +### Update integration mapping |
| 159 | + |
| 160 | +1. Go to the [data sources](https://app.getport.io/settings/data-sources) page of your portal. |
| 161 | +2. Select the AWS integration. |
| 162 | +3. Add the following YAML block into the editor to ingest storage data from AWS: |
| 163 | + |
| 164 | + <details> |
| 165 | + <summary><b>AWS integration configuration (Click to expand)</b></summary> |
| 166 | + |
| 167 | + ```yaml showLineNumbers |
| 168 | + deleteDependentEntities: true |
| 169 | + createMissingRelatedEntities: true |
| 170 | + enableMergeEntity: true |
| 171 | + resources: |
| 172 | + - kind: AWS::Organizations::Account |
| 173 | + selector: |
| 174 | + query: 'true' |
| 175 | + port: |
| 176 | + entity: |
| 177 | + mappings: |
| 178 | + identifier: .Id |
| 179 | + title: .Name |
| 180 | + blueprint: '"awsAccount"' |
| 181 | + properties: |
| 182 | + arn: .Arn |
| 183 | + email: .Email |
| 184 | + status: .Status |
| 185 | + joined_method: .JoinedMethod |
| 186 | + joined_timestamp: .JoinedTimestamp | sub(" "; "T") |
| 187 | + - kind: AWS::RDS::DBInstance |
| 188 | + selector: |
| 189 | + query: 'true' |
| 190 | + useGetResourceAPI: 'true' |
| 191 | + port: |
| 192 | + entity: |
| 193 | + mappings: |
| 194 | + identifier: .Identifier |
| 195 | + title: .Properties.DBInstanceIdentifier |
| 196 | + blueprint: '"rdsInstance"' |
| 197 | + properties: |
| 198 | + link: >- |
| 199 | + 'https://console.aws.amazon.com/go/view?arn=' + |
| 200 | + .Properties.DBInstanceArn |
| 201 | + dbInstanceClass: .Properties.DBInstanceClass |
| 202 | + dbInstanceStatus: .Properties.DBInstanceStatus |
| 203 | + engine: .Properties.Engine |
| 204 | + storageType: .Properties.StorageType |
| 205 | + engineVersion: .Properties.EngineVersion |
| 206 | + port: .Properties.Endpoint.Port |
| 207 | + allocatedStorage: .Properties.AllocatedStorage |
| 208 | + endpoint: .Properties.Endpoint.Address |
| 209 | + multiAZ: .Properties.MultiAZ |
| 210 | + deletionProtection: .Properties.DeletionProtection |
| 211 | + availabilityZone: .Properties.AvailabilityZone |
| 212 | + masterUsername: .Properties.MasterUsername |
| 213 | + publicAccess: .Properties.PubliclyAccessible |
| 214 | + vpcSecurityGroups: .Properties.VpcSecurityGroups |
| 215 | + arn: .Properties.DBInstanceArn |
| 216 | + instance_id: .Properties.InstanceId |
| 217 | + relations: |
| 218 | + account: .__AccountId |
| 219 | + ``` |
| 220 | + </details> |
| 221 | + |
| 222 | +4. Click `Save & Resync` to apply the mapping. |
| 223 | + |
| 224 | + |
| 225 | +## Set up automations |
| 226 | + |
| 227 | +We will create an automation that triggers when a new RDS instance is added to the catalog and violates security policies. |
| 228 | + |
| 229 | +### Create insecure RDS detection automation |
| 230 | + |
| 231 | +This automation monitors RDS instance creation and triggers remediation when security violations are detected: |
| 232 | + |
| 233 | +1. Go to the [automations](https://app.getport.io/settings/automations) page of your portal. |
| 234 | +2. Click on `+ Automation`. |
| 235 | +3. Click on the `{...} Edit JSON` button. |
| 236 | +4. Copy and paste the following JSON configuration: |
| 237 | + |
| 238 | + <details> |
| 239 | + <summary><b>Insecure RDS detection automation (Click to expand)</b></summary> |
| 240 | + |
| 241 | + :::tip Repository configuration |
| 242 | + Replace `<YOUR-IAC-REPOSITORY>` with your actual infrastructure repository name in the format `<github-org>/<repo-name>`. This should match the repository identifier used in your Service blueprint mapping. |
| 243 | + ::: |
| 244 | + |
| 245 | + ```json showLineNumbers |
| 246 | + { |
| 247 | + "identifier": "insecure_rds_creation", |
| 248 | + "title": "Insecure RDS Creation", |
| 249 | + "description": "Automation that remediates insecure RDS instances (missing deletion protection, storage encryption, or private access) using IaC.", |
| 250 | + "icon": "AmazonRDS", |
| 251 | + "trigger": { |
| 252 | + "type": "automation", |
| 253 | + "event": { |
| 254 | + "type": "ENTITY_CREATED", |
| 255 | + "blueprintIdentifier": "rdsInstance" |
| 256 | + }, |
| 257 | + "condition": { |
| 258 | + "type": "JQ", |
| 259 | + "expressions": [ |
| 260 | + ".diff.after.properties.deletionProtection == false", |
| 261 | + ".diff.after.properties.storageEncrypted == false", |
| 262 | + ".diff.after.properties.publicAccess == true" |
| 263 | + ], |
| 264 | + "combinator": "or" |
| 265 | + } |
| 266 | + }, |
| 267 | + "invocationMethod": { |
| 268 | + "type": "WEBHOOK", |
| 269 | + "url": "https://api.getport.io/v1/actions/run_claude_code/runs", |
| 270 | + "agent": false, |
| 271 | + "synchronized": true, |
| 272 | + "method": "POST", |
| 273 | + "headers": { |
| 274 | + "RUN_ID": "{{ .run.id }}", |
| 275 | + "Content-Type": "application/json" |
| 276 | + }, |
| 277 | + "body": { |
| 278 | + "properties": { |
| 279 | + "service": "<YOUR-IAC-REPOSITORY>", |
| 280 | + "prompt": "Here is the current configuration of the RDS instance: {{ .event.diff.after }}.\n\nGenerate a Terraform patch that remediates the following misconfigurations:\n1. Ensure the RDS instance is not publicly accessible (set publicly_accessible = false).\n2. Enable deletion protection (set deletion_protection = true).\n3. Ensure storage is encrypted (set storage_encrypted = true).\n\nThe Terraform must:\n- Be compatible with the existing AWS provider configuration.\n- Preserve existing identifiers (db_instance_identifier).\n- Only update the relevant security fields.\n- If an existing RDS file (e.g., rds.tf or main.tf) exists, append the fix there\n- If no such file exists, create a new file named rds_remediation.tf\nDo not overwrite unrelated files.\n\nAfter generating the code, open a PR with a description summarizing what was fixed and why." |
| 281 | + } |
| 282 | + } |
| 283 | + }, |
| 284 | + "publish": true |
| 285 | + } |
| 286 | + ``` |
| 287 | + </details> |
| 288 | + |
| 289 | +5. Click `Create` to save the automation. |
| 290 | + |
| 291 | +:::caution Automation scope |
| 292 | +This automation triggers on any RDS instance creation that violates security policies. You can modify the condition to be more specific or add additional security checks based on your organization's requirements. |
| 293 | +::: |
| 294 | + |
| 295 | + |
| 296 | +## Test the workflow |
| 297 | + |
| 298 | +To test the remediation workflow: |
| 299 | + |
| 300 | +1. Create (or ingest) a cloud resource such as RDS instance that violates one of the policies (e.g., `publicAccess = true`). |
| 301 | + |
| 302 | +2. Port will trigger the automation automatically. |
| 303 | + |
| 304 | +3. Claude Code generates a Terraform patch and opens a pull request in your repository. |
| 305 | + |
| 306 | +4. Review and merge the PR. |
| 307 | + |
| 308 | +<img src="/img/guides/auto-remediate-rds-instance.png" border="1px" width="70%" /> |
| 309 | + |
| 310 | + |
| 311 | +## Extend to other resource types |
| 312 | + |
| 313 | +You can extend this approach to other cloud resources by creating similar blueprints and automations: |
| 314 | + |
| 315 | +### S3 Bucket security |
| 316 | + |
| 317 | +Create an S3 bucket blueprint with properties like: |
| 318 | +- `publicAccessBlock` |
| 319 | +- `encryptionAtRest` |
| 320 | +- `versioningEnabled` |
| 321 | + |
| 322 | +### ElastiCache security |
| 323 | + |
| 324 | +Create an ElastiCache blueprint with properties like: |
| 325 | +- `deletionProtection` |
| 326 | +- `encryptionAtRest` |
| 327 | +- `networkType` |
| 328 | + |
| 329 | +### Security Group rules |
| 330 | + |
| 331 | +Create a security group blueprint to monitor for overly permissive rules: |
| 332 | +- `ingressRules` |
| 333 | +- `egressRules` |
| 334 | +- `cidrBlocks` |
| 335 | + |
| 336 | + |
| 337 | +## Related guides |
| 338 | + |
| 339 | +- [Trigger GitHub Copilot from Port](/guides/all/trigger-github-copilot-from-port) |
| 340 | +- [Trigger Google Gemini Assistant from Port](/guides/all/trigger-gemini-assistant-from-port) |
0 commit comments