Skip to content

How to prevent Greengrass V2 components from restarting during deployment if they haven’t changed?

0

``I’m working on an IoT deployment setup using AWS Greengrass V2, where multiple components (e.g., portal, heartbeat, log_scraper) are deployed to devices at edge sites.

When I make a deployment (e.g., updating just the portal component), all previously deployed components on the device get restarted, even if: Their versions haven’t changed.Their recipes and configuration are untouched.They are not included in the deployment changes.This causes unnecessary downtime and latency on the device.

Verified that only one component version is changing (e.g., only portal gets bumped).In the deployment JSON/payload, I’ve seen that all components still restart.

✅ Desired Behavior I want deployments to behave intelligently, such that: Only updated or new components get restarted. Unchanged components remain running and untouched.

below is my component

{
"RecipeFormatVersion": "2020-01-25",
"ComponentName": "com.heartbeatsiteComponent",
"ComponentVersion": "1.0.2",
"ComponentType": "aws.greengrass.generic",
"ComponentDescription": "Heartbeat site service container launched via Docker.",
"ComponentPublisher": "ram",
"ComponentConfiguration": {},
"ComponentDependencies": {
"aws.greengrass.DockerApplicationManager": {
"VersionRequirement": ">=2.0.0",
"DependencyType": "HARD"
},
"aws.greengrass.TokenExchangeService": {
"VersionRequirement": ">=2.0.0",
"DependencyType": "HARD"
}
},
"Manifests": [
{
"Platform": {
"os": "linux"
},
"Lifecycle": {
"run": "aws ecr get-login-password --region us-east-2 | docker login --username AWS --password-stdin ***.dkr.ecr.us-east-2.amazonaws.com && site_id=$(aws iot describe-thing --thing-name "$AWS_IOT_THING_NAME" | jq -r '.attributes.site_id') && aws s3 cp s3:///.env /tmp/site.env && aws s3 cp *******/prod.env /tmp/prod.env && set -a && . /tmp/site.env && set +a && docker run -d --name heartbeat_site --network=host --restart=always --env-file /tmp/prod.env -e AWS_CONTAINER_CREDENTIALS_FULL_URI=$AWS_CONTAINER_CREDENTIALS_FULL_URI -e AWS_CONTAINER_AUTHORIZATION_TOKEN=$AWS_CONTAINER_AUTHORIZATION_TOKEN -e PYTHONUNBUFFERED=1 -e SITE_ID=$site_id -e ROLE=site

-e ROLE=site  $REPO_ROOT/$HEARTBEAT_IMAGE:$HEARTBEAT_VERSION  && tail -f /dev/null",
"shutdown": "docker stop heartbeat_site && docker rm heartbeat_site"
      },
      "Artifacts": []
    }
  ],
  "Lifecycle": {}
}
1 Answer
0

In AWS IoT Greengrass V2, components will restart during deployment even if they haven't changed, but there are several approaches to prevent this behavior:

  1. Use the DeploymentComponentUpdatePolicy: When creating your deployment, you can set the update policy action to NOTIFY_COMPONENTS. This allows components to receive notifications before they're stopped and updated. Components can then use the SubscribeToComponentUpdates IPC operation to receive these notifications and respond with the DeferComponentUpdate IPC operation.

  2. Component State Management: Design your components to maintain their own state. Your component can check if it has already run and decide whether to execute its main logic or simply exit. This can be implemented by having the component write a status to a file or database after the first run, and on subsequent runs, check this state and exit if it determines it should not run again.

  3. Use the Skipif Lifecycle Field: In your component recipe, you can use the Skipif lifecycle field to conditionally skip execution based on certain criteria. For example:

{
"Lifecycle": {
"Run": {
"Script": "python3 /path/to/your/script.py",
"Skipif": "[ -f /path/to/marker.file ]"
}
}
}
  1. Leverage Component Versioning: Design your components to only run once per version. After a deployment, it would not run again unless a new version of the component is deployed.

  2. Modify Environment Variables: Greengrass restarts a component when its Setenv configuration changes. You can use this behavior to your advantage by only changing environment variables for components you want to restart, while keeping them the same for components you want to keep running.

Remember that when you create a new deployment for a target that already has a deployment, the components in the new deployment replace those from the previous deployment. If you want to preserve existing components, consider using the "revise deployments" approach, which allows you to update only specific components without removing others that are already running on the core devices.
Sources
How do I redeploy the same deployment config in Greengrass v2 | AWS re:Post
DeploymentComponentUpdatePolicy - AWS IoT Greengrass
How to make a greengrass component run only once? | AWS re:Post
Revise deployments - AWS IoT Greengrass

answered 4 months ago
  • (1) Would work if components can determine a good time to restart (e.g. right after sending a heartbeat, emptying most or all of the logs). (2) is probably not applicable since these are long-running services. (3) N/A (Component would be stopped and never run again) (4) N/A (Component isn't having its version bumped) (5) Not sure if this is a culprit.

  • nothing worked

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.