Skip to main content

AWS flock

flock-like functionality for applications in AWS, using dynamodb as a backend for synchronization.

A CLI tool as simple as the classic flock command.

Use Cases

  • Synchronizing multiple automated jobs across workers in AWS
  • Writing scripts that only one human operator can be running at a time (e.g. production deployment tools with terraform, CFN, etc)
  • Synchronizing jobs in non-AWS systems (e.g. GitHub Actions) using AWS credentials

Usage

Full usage info can be found with

awsflock --help

Create the table in DynamoDB in order to start using awsflock:

awsflock table-create

Acquire a lock, with a 15 minute expiration, and get the lock LEASE_ID:

LEASE_ID="$(awsflock acquire LockFoo --lease-duration '15 minutes')"

Renew the lock, getting back the new LEASE_ID and reducing the expiration window to 5 minutes, specified in seconds:

LEASE_ID="$(awsflock renew LockFoo "$LEASE_ID" --lease-duration '300s')"

Release the lock, so that others may use it:

awsflock release LockFoo "$LEASE_ID"

Attempt to acquire another lock, but don't block and wait for it to be acquired:

LEASE_ID="$(awsflock acquire LockBar --no-wait)"
if [ $? -eq 0 ]; then
  # lock acquired ...
else
  # lock not acquired ...
fi

Behavior / Model

Locks are held for a limited period of time. After that time, if not renewed, the lock expires and another worker may reclaim the lock. While the lock is held, the worker holding it has a "lease" on the lock, proven by a given LEASE_ID. A LEASE_ID can be used to "renew" or "release" a given lock.

awsflock requires a table in DynamoDB to store active locks. By default, the table name is awsflock, but custom names can be used. You must create the table before locks can be used (it will never be created automatically). Locks are identified by name, and those names are unique keys into the awsflock table.

Locks have a limited lifetime (default: 2 hours) if not explicitly released, after which they may be "reclaimed" by anyone trying to acquire that lock.

NOTE: Local clock time is compared against lock expirations to determine whether or not reclamation may be tried. The default reclamation window (5 seconds) is more than sufficient for most use-cases, but assumes that your clocks are synchronized by NTP or a similar protocol. Usage where clocks cannot be trusted may result in incorrect lock reclamations.

When you acquire a lock, you get back a LEASE_ID. The LEASE_ID can then be used to renew the lock or release it. In this way, locks are held by a single owner for a limited period of time, and the LEASE_ID constitutes proof of ownership.

CHANGELOG

0.2.0

  • More advanced parsing of durations

0.1.1

  • Minor fixup

0.1.0

  • Initial release

Release files for awsflock 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for awsflock 0.2.0
File Size Uploaded
awsflock-0.2.0.tar.gz 13.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for awsflock 0.2.0
File Interpreter ABI Platform
awsflock-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 28.6 kB

Release files / awsflock-0.2.0.tar.gz

Download URL awsflock-0.2.0.tar.gz
Size 13.6 kB
Tags Source
SHA-256 checksum
How to use checksums
2d255cea0f96a730cb1b26e8f25fc2a8627f643d53d89fd3bcd1dd790925ca42
BLAKE2b-256 checksum
How to use checksums
41342a4a4900f80fc2bea4681cb4551ee71fba496e035eecf2a687fcef7d044b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.4 CPython/3.6.9 Linux/5.3.0-1032-aws

Release files / awsflock-0.2.0-py3-none-any.whl

Download URL awsflock-0.2.0-py3-none-any.whl
Size 15.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1d457f2b563bc12ebc4e2ae187ca25a09d41848978ad08543c7a571564039753
BLAKE2b-256 checksum
How to use checksums
8732e3f7ba955842a830af668470e7ac8cac23da545cd091f67b0e1eedfb6104
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.4 CPython/3.6.9 Linux/5.3.0-1032-aws

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page